How to Make JavaScript Sleep or Wait? Last Updated : 27 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.1. Using setTimeout() and PromisesThe setTimeout() function is used to schedule a function to run after a specified delay. By combining it with Promises, we can create a custom sleep() function that pauses code execution for a defined time. JavaScript async function main() { console.log("Before sleep"); await sleep(2000); // Sleep for 2 seconds console.log("After sleep [After 2 Seconds]"); } function sleep(time) { return new Promise((resolve) => setTimeout(resolve, time)); } main(); OutputHow to Make JavaScript Sleep or Wait2. Using async/await KeywordYou can simplify the code and improve readability by using async/await with the sleep function. The await keyword pauses the execution of the function until the Promise returned by the sleep function is resolved. JavaScript async function main() { console.log("Before sleep"); await sleep(5000); // Sleep for 5 seconds console.log("After sleep [After 5 Seconds]"); } async function sleep(ms) { await new Promise((resolve) => setTimeout(resolve, ms)); } main(); OutputHow to Make JavaScript Sleep or WaitBoth approaches provide ways to pause code execution for a specified duration, allowing us to control the flow of our JavaScript programs. Whether you choose the setTimeout() and Promises approach or the async/await approach depends on your preference and the requirements of your project. Comment More infoAdvertise with us Next Article How to Make JavaScript Sleep or Wait? G gpancomputer Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Methods JavaScript-Questions Similar Reads How to Wait n Seconds in JavaScript? Here are the various methods to wait n seconds in JavaScript1. Using setTimeout() FunctionsetTimeout() is an asynchronous function in JavaScript that allows you to run code after a specific amount of time has passed. Since setTimeout() is a method of the window object, you can technically write it a 3 min read JavaScript Atomics wait() Method The Atomics.wait() in JavaScript is an in-built method that is used to verify whether a given position in an Int32Array still contains a given value and if so sleeps, awaiting a wakeup or a timeout. The Atomics.wait() operation returns a string that is either "ok", "not-equal", or "timed-out". The i 2 min read How to set the cursor to wait in JavaScript ? In JavaScript, we could easily set the cursor to wait. In this article, we will see how we are going to do this. Actually, it's quite an easy task, there is a CSS cursor property and it has some values and one of the values is wait. We will use the [cursor: wait] property of CSS and control its beha 3 min read JavaScript setTimeout() Method JavaScript setTimeout() method allows you to schedule the execution of a function or the evaluation of a code after a specified delay. The setTimeout() method calls a function after several milliseconds. setTimeout() is for executing a function once after a specified delay. Syntax:setTimeout(functio 2 min read JavaScript setInterval() Method The setInterval() method calls a function at specified intervals (in milliseconds). It continues calling the function until clearInterval() is called or the window is closed. This method is useful for tasks that need periodic execution, like updating animations or refreshing data. Important Note: se 2 min read Like