开发者

Do something when a for loop ends

开发者 https://www.devze.com 2023-01-29 10:02 出处:网络
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:

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

精彩评论

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