Skip to content
On this page

sleep 阻塞代码运行

js
// 阻塞代码运行
// alert()
function sleep(block) {
    let prev = Date.now(), now = null
    while(true) {
        now = Date.now()
        if(now - prev > block) break;
    }
}
sleep(1000)
console.log(1) // 阻塞1000ms后输出
// 阻塞代码运行
// alert()
function sleep(block) {
    let prev = Date.now(), now = null
    while(true) {
        now = Date.now()
        if(now - prev > block) break;
    }
}
sleep(1000)
console.log(1) // 阻塞1000ms后输出