来,把settimeout的代码改成我们想要的结果

const obj = {

    name: " jsCoder",

    skill: ["es6", "react", "angular"],

    say: function () {

        for (var i = 0, len = this.skill.length; i < len; i++) {

            setTimeout(function(){

                console.log(i);

                console.log(this.skill[i]);

            }(i), 0)

            console.log(i);

        }

    }

};

obj.say();

改成settimeout中可以正常顺序打印i和依次打印skill,大家都懂的,有哪些方法,各位发挥


小唯快跑啊
浏览 537回答 4
4回答

交互式爱情

const obj = {&nbsp; &nbsp; name: " jsCoder",&nbsp; &nbsp; skill: ["es6", "react", "angular"],&nbsp; &nbsp; say: function () {&nbsp; &nbsp; &nbsp; &nbsp; this.skill.forEach(function(item,index){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(index)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(item);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },0)&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; },&nbsp; &nbsp; say2:function(){&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0, len = this.skill.length; i < len; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function(i,item){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(item);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },0,i,this.skill[i]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; say3:function(){&nbsp; &nbsp; &nbsp; &nbsp; const arr=this.skill.slice();&nbsp; &nbsp; &nbsp; &nbsp; let i=0;&nbsp; &nbsp; &nbsp; &nbsp; function pop(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const item=arr.shift();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(item){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(item);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(pop,0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(pop,0);&nbsp; &nbsp; }};obj.say();setTimeout(function(){&nbsp; &nbsp; obj.say2();},1000)setTimeout(function(){&nbsp; &nbsp; obj.say3();},2000)

慕妹3242003

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; say: function ()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var that = this;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0, len = this.skill.length; i < len; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(that.skill[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }(i), 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

一只名叫tom的猫

say: function () {&nbsp; &nbsp; for (var i = 0, len = this.skill.length; i < len; i++) {&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(((i) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return () => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(this.skill[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })(i), 0)&nbsp; &nbsp; &nbsp; &nbsp; console.log(i);&nbsp; &nbsp; }}

MM们

function passTheRiver(){const obj = {&nbsp; &nbsp; "count":0,&nbsp; &nbsp; name: " jsCoder",&nbsp; &nbsp; skill: ["es6", "react", "angular"],&nbsp; &nbsp; say: function () {&nbsp; &nbsp; &nbsp; &nbsp; var that = this;&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0, len = this.skill.length; i < len; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(that.count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(that.skill[that.count]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; that.count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }};obj.say();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript