Promise
function sleep(time = 1) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(true)
}, time);
})
}
while循环
function sleep(time = 1) {
let start = +Date.now()
while(true) {
if (+Date.now() - start >= time) break
}
}
for循环
function sleep(time = 1) {
for(let t = Date.now(); Date.now() - t <= time;) {}
}