JavaScript Sleep Function – Linux Hint


Javascript is the language of freedom but is a function-oriented language on the identical time. Not like different languages, javascript doesn’t present a built-in sleep() operate. You may both construct a customized sleep() operate utilizing the built-in setTimeout() operate, or the newest ECMAScript guarantees an async-await operate. This text reveals you the way to cease or pause the execution of the sleep operate for a desired period of time utilizing guarantees or async-await features.

Earlier than Beginning

Earlier than you begin to construct a sleep operate, you could perceive that the setTimeout() operate n’t work in case you count on it to cease the execution. Many programmers and builders attempt to use the operate with loops however fail as a result of the setTimeout() operate is used to attend for some given period of time after which runs the given operate. You may, nevertheless, use the setTimeout() operate to construct a sleep operate utilizing promise in case your function is to cease the execution for a desired period of time.

Utilizing the Sleep Operate

So, we’ll make a customized sleep operate during which the operate will get time in milliseconds as an argument and return a promise. The promise will embrace a setTimeout() operate, which is able to move the resolver as a operate and time in milliseconds to the setTimeout() operate. So, in the long run, the sleep operate ought to appear like this:

operate sleep(ms){
return new Promise( resolver => setTimeout(resolver, ms));

And now, wherever you wish to use this sleep operate, you may simply use it.

Now, we’ll use this sleep operate in a few examples to indicate you the way to use it in apply.

First, we’ll attempt to console some textual content and name the sleep operate. For the reason that sleep operate is returning a promise, we put a then operate after it, during which we’ll console some textual content and move the argument ‘5000’ to the sleep operate. After operating this system, you will notice within the console that it’ll sleep for five seconds.

console.log(“Sleep operate will await 10 seconds after which it’ll print ‘Performed’”);


You may witness the delay of 5 seconds to get to the “Performed” standing within the console.

Suppose we wish to carry out an animation after each 2 seconds. To take action, we’ll merely write an asynchronous animation operate, during which we’ll animate one thing, cease the execution for two seconds utilizing sleep, after which repeat this course of utilizing a for loop for 10 occasions.

async operate animation(ms){
for (let i = 0; i < 10; i++) {
console.log(“animation after 2 seconds…”)
await sleep(ms)
console.log(“That is the tip.”);

After writing the asynchronous animation operate, we are able to now name the animation operate.

After operating the code, you will notice within the console that the textual content “animation after 2 seconds” is repeating each two seconds.


This text confirmed you the way to make a customized sleep operate, alongside a number of demonstrations. I hope this text has helped you to higher perceive the utilization of sleep operate. You may be taught extra about Javascript at

call php function every 10 seconds,php sleep vs wait,php usleep,php delay redirect,sleep function in php,how to call a function after some time in php

You May Also Like