开发者

Do something when a for loop ends

I have a for loop in JavaScript, and I wan开发者_如何转开发t some code to run when it ends. This works, but I'm wondering if there are any simpler solutions:

for (var i = 1; i <= 100; i++) {
    console.log('Line ' + i);

    if (i === 100) {
        alert('Loop ended!');
    }
}


for (var i = 1; i <= 100; i++) {
    console.log('Line ' + i);
}

alert('Loop ended!');


unless I'm missing something, what's wrong with:

for (var i = 1; i <= 100; i++) {
    console.log('Line ' + i);
}

alert('Loop ended!');


why dont you do like this?

Array.from(Array(100), (_, i) => console.log('Line ' + (i + 1)));
alert('Loop ended!');


Try this

for (var i = 1; i <= 100; i++) {
   console.log('Line ' + i);
}

alert('');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜