javascript - 关于js,alert()阻塞的问题
天蓬老师
天蓬老师 2017-04-17 16:38:12
0
2
662

我们知道alert()是阻塞式的,但是一下代码执行顺序有问题

执行顺序有问题

(()=>{
    for(let i = 0; i < 5; i++){
        setTimeout(() => alert(i), 1000);
    }
})()

结果: 0,3,2,1,4

执行顺序没问题

(()=>{
    for(let i = 0; i < 5; i++){
        setTimeout(() => console.log(i), 1000);
    }
})()

结果:0,1,2,3,4

这是为什么呢?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
PHPzhong

It is recommended to take a look at the operating principle of setTimeout

If you don’t click OK when alert 0, wait 5s (if the thread is idle, you may need to wait 10s or more) and then click again, you will find that the order is normal

The reason is that setTimeout is in the code. If the process is occupied, when the process is idle, it will skip this time and execute the next time

大家讲道理

Post a link, you should be enlightened after reading it

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template