Javascript sleep function

Javascript sleep function

How to make a function sleep for a certain period of time in Javascript?

Sleep() function

With the help of the Sleep() function, we can create a function to interrupt the execution of the program for a certain period of time.
The problem is that in Javascript there are no sleep() functions like in other programming languages :

For example in C and PHP, there is the sleep(sec) function. In java, it is possible to do a sleep using threads. In python, there is the time library which contains a sleep() function.

There is, however, a way to make a sleep function in
using the async/await function in ES 2018. These functions allow us to use the sleep() function as easily as possible. Here is how it works:

const sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds))
}

We can use the sleep function with then call back :

sleep(500).then(() => {
  //your code
})

Or you can use it in an asynchronization function, as shown below:

const Snorlax = async () => {
  await sleep(2000)
  //your code
}

Snorlax()

Example of the sleep() function

In the following example, we have used the sleep() with async/await function. Here sleep() function is accompanied with await to continue the proceedings. Initially the text in the async function “Hello Pokemon Trainer Your snorlax is starting to sleep…” is displayed once the function is started. Later on, the function is paused using sleep function for 1 second. Once the time period is completed, the text(“Snorlax has been asleep for X seconds now…“) following the sleep() function is displayed. The X value is incremented at each pause made by the sleep() function. It is repeated until the loop terminates, meaning that in total the text is going to be repeated for 14 times as shown in the output.

function sleep(ms) {
      return new Promise(resolve => setTimeout(resolve, ms));
   }
   async function snorlax() {
      document.write('Hello Pokemon Trainer </br> Your snorlax is starting to sleep... </br>');
      for (let i = 1; i<15 ; i++) {        
         await sleep(1000);
         document.write("Snorlax has been asleep for "+i+" seconds now...</br>");
      }
   }
   snorlax()

You can try this example by going through this site that allows you to execute javascript online:

https://www.webtoolkitonline.com/javascript-tester.html

Output :

Hello Pokemon Trainer
Your snorlax is starting to sleep...
Snorlax has been asleep for 1 seconds now...
Snorlax has been asleep for 2 seconds now...
Snorlax has been asleep for 3 seconds now...
Snorlax has been asleep for 4 seconds now...
Snorlax has been asleep for 5 seconds now...
Snorlax has been asleep for 6 seconds now...
Snorlax has been asleep for 7 seconds now...
Snorlax has been asleep for 8 seconds now...
Snorlax has been asleep for 9 seconds now...
Snorlax has been asleep for 10 seconds now...
Snorlax has been asleep for 11 seconds now...
Snorlax has been asleep for 12 seconds now...
Snorlax has been asleep for 13 seconds now...
Snorlax has been asleep for 14 seconds now... 

If you have any questions about the sleep() function, don’t hesitate to leave a comment below!

Back to the Javascript section

By ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Leave a comment

Your email address will not be published. Required fields are marked *