亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JS對象賦值無效?

JS對象賦值無效?

尚方寶劍之說 2018-10-20 10:14:41
本意:循環執行3個函數,來改變ul節點的className,2秒后執行wait,3秒后執行stop,1秒后執行pass,依次循環。HTML部分 <ul id="traffic" class="wait">     <li><span></span></li>      <li><span></span></li>      <li><span></span></li>   </ul>JS部分var statusList=[      {        func:function(){          traffic.className='wait';        },        timer:2000      },      {        func:function(){          traffic.className='stop';        },        timer:3000      },      {        func:function(){          traffic.className='pass';        },        timer:1000      }      ];          var currentIndex = 0;    var statusObj=statusList[currentIndex];    setInterval(        function(){            statusObj.func();            debugger;            currentIndex=(currentIndex+1)%statusList.length;            console.log(currentIndex);        },        statusObj.timer    );即使右邊的statusList[currentIndex]在改變,statusObj變量一直是statusList[0],哪里有問題?
查看完整描述

1 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

哥們,你只更新了下標序號,沒有更新statusObj對象啊,它只被賦值了一次,就是數組內的第一個元素啊,你可以將

 function(){
                statusObj.func();                debugger;
                currentIndex=(currentIndex+1)%statusList.length;                console.log(currentIndex);
            }

改成

function(){
                statusObj.func();                debugger;
                currentIndex=(currentIndex+1)%statusList.length;
                statusObj = statusList[currentIndex];                console.log(currentIndex);
            }


查看完整回答
反對 回復 2018-11-08
  • 1 回答
  • 0 關注
  • 1007 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號