🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 简介 模拟其他编程语言实现 sleep 函数。它能让程序挂起一段时间,然后再执行下面的逻辑。 <br> <br> # 实现 ~~~ function sleep (time) { return new Promise(resolve => { setTimeout(resolve, time) }) } ~~~ <br> <br> # 调用 ## es6 ~~~ console.log('Do some thing, ' + new Date()); sleep(3000).then(() => { console.log('Do other things, ' + new Date()); }) ~~~ <br> ## es7 ~~~ (async () => { console.log('Do some thing, ' + new Date()) await sleep(3000) console.log('Do other things, ' + new Date()) })() ~~~