JavaScript Thread.Sleep()
What is the JavaScript version of sleep()?
需要注意的是,这里必须await才会等待
Since 2009 when this question was asked, JavaScript has evolved significantly. All other answers are now obsolete or overly complicated. Here is the current best practice:
function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function demo() { console.log('Taking a break...'); await sleep(2000); console.log('Two seconds later, showing sleep in a loop...'); // Sleep in loop for (let i = 0; i < 5; i++) { if (i === 3) await sleep(2000); console.log(i); } } demo();
This is it. await sleep(<duration>)
.
Note that,
await
can only be executed in functions prefixed with theasync
keyword, or at the top level of your script in some environments (e.g. the Chrome DevTools console, or Runkit).await
only pauses the currentasync
function
Two new JavaScript features helped write this "sleep" function:
- Promises, a native feature of ES2015 (aka ES6). We also use arrow functions in the definition of the sleep function.
- The
async/await
feature lets the code explicitly wait for a promise to settle (resolve or reject).
Compatibility
- promises are supported in Node v0.12+ and widely supported in browsers, except IE
async
/await
landed in V8 and has been enabled by default since Chrome 55 (released in Dec 2016)- it landed in Node 7 in October 2016
- and also landed in Firefox Nightly in November 2016
If for some weird reason you're using Node older than 7 (which has reached end of life), or are targeting old browsers, async
/await
can still be used via Babel (a tool that will transpile JavaScript + new features into plain old JavaScript), with the transform-async-to-generator
plugin.
What's the equivalent of Java's Thread.sleep() in JavaScript? [duplicate]
The simple answer is that there is no such function.
The closest thing you have is:
var millisecondsToWait = 500;
setTimeout(function() {
// Whatever you want to do after the wait
}, millisecondsToWait);
Note that you especially don't want to busy-wait (e.g. in a spin loop), since your browser is almost certainly executing your JavaScript in a single-threaded environment.
Here are a couple of other SO questions that deal with threads in JavaScript:
And this question may also be helpful:
How to wait 5 seconds with jQuery?
Built in javascript setTimeout.
setTimeout(
function()
{
//do something special
}, 5000);
UPDATE: you want to wait since when the page has finished loading, so put that code inside your $(document).ready(...);
script.
UPDATE 2: jquery 1.4.0 introduced the .delay
method. Check it out. Note that .delay only works with the jQuery effects queues.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-07-12 网上职位要求对照
2018-07-12 Use of implicitly declared global variable
2018-07-12 ResolveUrl in external JavaScript file in asp.net project
2018-07-12 data-toggle data-target
2017-07-12 字符编码
2015-07-12 Which are in?
2015-07-12 IQ Test