About 246,000 results
Open links in new tab
  1. What is the JavaScript version of sleep()? - Stack Overflow

    Jun 4, 2009 · Two new JavaScript features (as of 2017) 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).

  2. How to Make JavaScript Sleep or Wait - Built In

    Oct 29, 2024 · To create a sleep function in JavaScript, use the Promise, await and async functions in conjunction with setTimeout(). Await causes the code to wait until the promise object is fulfilled, operating like a sleep function. You can also pass staggered, increasing setTimeout() functions to simulate a sleep function.

  3. Is there a sleep function in JavaScript? - Stack Overflow

    Jul 17, 2009 · The answers are not good, the simplest and best answer is: function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } and you can use this like: await new Promise(r => setTimeout(r, 2000));

  4. How to Make JavaScript Sleep or Wait? - GeeksforGeeks

    Nov 27, 2024 · In JavaScript, there is no built-in sleep function like in some other programming languages. However, you can create a delay or pause in code execution by using asynchronous methods. We can use setTimeout() or Promise along with async/await to achieve this functionality for more readable code.

  5. Delay, Sleep, Pause & Wait in JavaScript - SitePoint

    Sep 7, 2023 · Learn how to create a sleep function in JavaScript for pausing code execution, given no built-in sleep() function for delaying program flow.

  6. JavaScript Sleep Function - Online Tutorials Library

    Learn how to implement a sleep function in JavaScript to pause execution in your code for a specified duration.

  7. Create a sleep function using JavaScript - with code examples

    Apr 21, 2022 · The more readable and clean way to delay execution in JavaScript is to create your own sleep() function. By using promises and async / await features of JavaScript, you can define a sleep() function as follows:

  8. JavaScript sleep - Stack Overflow

    A Javascript sleep() function (especially the way it's been implemented in another answer here!) would basically have the effect burn up your CPU cycles while it waited for the clock.

  9. JavaScript Sleep Function Explained: Async, Promises, and …

    Dec 30, 2024 · Learn how to implement a JavaScript sleep function using async/await, Promises, and setTimeout. Optimize your code for delay execution and performance.

  10. How to Sleep in JavaScript (The Right Way) - Medium

    Apr 3, 2025 · “JavaScript doesn’t have a built-in sleep () function like Python or C++.” But don’t worry— you can still pause execution in a clean, modern way using Promises and async/await. or you can use...

  11. Some results have been removed
Refresh