JavaScript/sleep:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第11行: 第11行:


同步执行
同步执行
 
{{leftrightflex|<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
function sleep (time) {
function sleep (time) {
   return new Promise((resolve) => setTimeout(resolve, time));
   return new Promise((resolve) => setTimeout(resolve, time));
第21行: 第20行:
   await sleep(3000);
   await sleep(3000);
   console.log('Do other things, ' + new Date());
   console.log('Do other things, ' + new Date());
})();</syntaxhighlight>
})();</syntaxhighlight>|{{consoleout|<pre>
Do some thing, Wed Jun 24 2020 23:42:28 GMT+0800 (中国标准时间)
Promise {<pending>}
VM199:8 Do other things, Wed Jun 24 2020 23:42:31 GMT+0800 (中国标准时间)
</pre>}}}}
 
{{unknown}}
{{unknown}}

2020年6月24日 (三) 16:43的版本

引用

模拟

function sleep (time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}

sleep(500).then(() => {
  // ...
})

同步执行 模板:Leftrightflex

(来源摘自互联网示例,作者 未知)