查看“︁JavaScript/sleep”︁的源代码
来自康健生活
←
JavaScript/sleep
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 引用 == == 模拟 == === es5 === {{Double-columns|<syntaxhighlight lang="javascript"> var sleep = function(time) { var startTime = new Date().getTime() + parseInt(time, 10); while(new Date().getTime() < startTime) { console.log('mark~'); } }; sleep(1000); </syntaxhighlight>|{{consoleout|<pre> 43322 mark~ </pre>}}}} 进程阻塞、性能和负载问题 === es6 === <syntaxhighlight lang="javascript"> function sleep (time) { return new Promise((resolve) => setTimeout(resolve, time)); } sleep(500).then(() => { // ... })</syntaxhighlight> {{Double-columns|<syntaxhighlight lang="javascript"> function* sleep(ms){ yield new Promise(function(resolve,reject){ console.log('mark~'); setTimeout(resolve,ms); }) } sleep(500).next().value.then(()=>{ console.log('mark,end~'); })</syntaxhighlight>|{{consoleout|<pre> mark~ Promise {<pending>} mark,end~ </pre>}}}} 同步执行 {{Double-columns|<syntaxhighlight lang="javascript"> function sleep (time) { return new Promise((resolve) => setTimeout(resolve, time)); } (async function() { console.log('Do some thing, ' + new Date()); await sleep(3000); console.log('Do other things, ' + new Date()); })();</syntaxhighlight>|{{consoleout|<pre> Do some thing, Wed Jun 24 2020 23:42:28 GMT+0800 (中国标准时间) Promise {<pending>} Do other things, Wed Jun 24 2020 23:42:31 GMT+0800 (中国标准时间) </pre>}}}} {{Double-columns|<syntaxhighlight lang="javascript"> function sleep (time) { return new Promise((resolve) => setTimeout(resolve, time)); } async function usetest() { var temple=await sleep(1000); console.log('mark~'); return temple } usetest();</syntaxhighlight>|{{consoleout|<pre> Promise {<pending>} mark~ </pre>}}}} {{unknown}} == co == <syntaxhighlight lang="javascript"> function sleep(ms) { return function (cb) { setTimeout(cb, ms); } } co(function *() { var now = Date.now(); yield sleep(2000); expect(Date.now() - now).to.not.be.below(2000); }); </syntaxhighlight>
此页面嵌入的页面:
模板:Consoleout
(
查看源代码
)
模板:Double-columns
(
查看源代码
)
模板:Double-columns/style.css
(
查看源代码
)
模板:Unknown
(
查看源代码
)
返回
JavaScript/sleep
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。