Introduction
Sometimes, we may wish to sleep()
for a specific period in a JavaScript program. In this post, we'll learn how to do it.
How to hold running JavaScript program for a specific time (alternative to sleep()
function in other programming languages)
To sleep()
your application in JavaScript you can use Promise
, setTimeout()
and await
:
This sleep()
function will hold the running program for a given period in milliseconds.
If you want to run code after a specific time you can use the following code:
In case you don't want to use the setTimeout()
function you can use the following:
Conclusion
In this post, we presented the best way to sleep() in JavaScript.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}