国外网站做推广,厦门软件公司排名,对口网站怎么做,wordpress 优化原理起因#xff1a; 只使用 setTimeout 会产生嵌套等方面的问题#xff0c;达不到想要的效果。
解决方法#xff1a; 使用 async/await 还有 Promise 相结合的方式来解决问题。
直接上代码#xff1a;
function sleep(time) {return new Promise((resolve) setTimeout…起因 只使用 setTimeout 会产生嵌套等方面的问题达不到想要的效果。
解决方法 使用 async/await 还有 Promise 相结合的方式来解决问题。
直接上代码
function sleep(time) {return new Promise((resolve) setTimeout(resolve, time));
}使用方式 一定要注意用 async/await
var a 1;var t setInterval(async function () {console.log(-------)console.log(前面, a)await sleep(1000);console.log(中间)await sleep(2000);console.log(后面, a)if (a 5) { clearInterval(t) }
}, 5000)function sleep(time) {return new Promise((resolve) setTimeout(resolve, time));
}结果