关于javascript的setinterval异步问题

第一段代码

https://img1.mukewang.com/5c837bb50001614002410230.jpg

运行结果为正常
https://img2.mukewang.com/5c837bbc0001d03d02310208.jpg

第二段代码

https://img3.mukewang.com/5c837bc40001f60202660308.jpg

运行结果却和预期不一样
https://img1.mukewang.com/5c837bce0001076202170302.jpg

请问为什么会这样??setInterval是异步执行的吗


哆啦的时光机
浏览 1318回答 2
2回答

蓝山帝景

var timer = nullvar a = [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index:1,&nbsp; &nbsp; &nbsp; &nbsp; },{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index:10,&nbsp; &nbsp; &nbsp; &nbsp; },{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index:20,&nbsp; &nbsp; &nbsp; &nbsp; },{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index:30,&nbsp; &nbsp; &nbsp; &nbsp; },{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index:40,&nbsp; &nbsp; &nbsp; &nbsp; }]timer = setInterval(function(){&nbsp; &nbsp; &nbsp; &nbsp; for(var ll=0;ll<a.length;ll++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[ll].index -= 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; console.log( JSON.stringify( a ) )},3500);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript