JavaScript中的睡眠-动作之间的延迟

JavaScript中的睡眠-动作之间的延迟

在JavaScript执行另一个操作之前,有什么方法可以让我在JavaScript中睡觉吗?

例子:

 var a = 1+3;
 // Sleep 3 seconds before the next action here
 var b = a + 4;


狐的传说
浏览 667回答 3
3回答

MMMHUHU

万一你真的需要sleep()只是为了测试一些东西。但是请注意,在调试过程中,它大多数情况下都会使浏览器崩溃-这可能就是您需要它的原因。在生产模式中,我将注释掉这个函数。function&nbsp;pauseBrowser(millis)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;date&nbsp;=&nbsp;Date.now(); &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;curDate&nbsp;=&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curDate&nbsp;=&nbsp;Date.now(); &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;while&nbsp;(curDate-date&nbsp;<&nbsp;millis);}不要用new Date()在循环中,除非您想浪费内存、处理能力、电池以及可能的设备寿命。

尚方宝剑之说

ECMAScript 6版本,使用用于“代码阻塞”的生成器:因为最初的问题是七年前发布的,所以我没有费心回答确切的代码,因为它太简单了,而且已经回答了。这将有助于解决更复杂的问题,例如,如果您至少需要两次睡眠,或者您计划对异步执行进行排序。请随意修改它以满足您的需要。let sleeptime = 100function* clock(){&nbsp; &nbsp; let i = 0&nbsp; &nbsp; while( i <= 10000 )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; i++&nbsp; &nbsp; &nbsp; &nbsp; console.log(i); // actually, just do stuff you wanna do.&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ()=>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clk.next()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , sleeptime&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; yield&nbsp; &nbsp; }}let clk = clock()clk.next()职能*()=>箭头函数您还可以通过承诺:function sleep(ms){&nbsp; &nbsp; return(&nbsp; &nbsp; &nbsp; &nbsp; new Promise(function(resolve, reject)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function() { resolve(); }, ms);&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; );}sleep(1000).then(function(){&nbsp; &nbsp; console.log('1')&nbsp; &nbsp; sleep(1000).then(function()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; console.log('2')&nbsp; &nbsp; })})或者简单得多,也不那么花哨。function sleep(ms, f){&nbsp; &nbsp; return(&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(f, ms)&nbsp; &nbsp; )}sleep(500, function(){&nbsp; &nbsp; console.log('1')&nbsp; &nbsp; sleep(500, function()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; console.log('2')&nbsp; &nbsp; })})console.log('Event chain launched')如果你只是在等待某种情况的发生,你可以这样等待function waitTill(condition, thenDo){&nbsp; &nbsp; if (eval(condition))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; thenDo()&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; setTimeout(&nbsp; &nbsp; &nbsp; &nbsp; ()&nbsp; &nbsp; =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; waitTill(condition, thenDo)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; )}x=0waitTill(&nbsp; &nbsp; 'x>2 || x==1'&nbsp; &nbsp; ,&nbsp; &nbsp; ()&nbsp; &nbsp; =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; console.log("Conditions met!")&nbsp; &nbsp; })// Simulating the changesetTimeout(&nbsp; &nbsp; () =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; x = 1&nbsp; &nbsp; }&nbsp; &nbsp; ,&nbsp; &nbsp; 1000)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript