两种方法实现 sleep 函数

async function sleep(delay) {
    await new Promise(resolve => setTimeout(resolve, delay))
}

// 可以链式调用
function sleep1(delay) {
    let curTime = Date.now()
    while(Date.now() - curTime < delay ) {
        // 
    }
}

sleep(2000).then(() => console.log('1'))
console.log(2)
sleep1(1000)
console.log(3)

你可能感兴趣的:(javascript,前端,vue.js)