/    Sign up×
Bounties /Pin to ProfileBookmark

What is the JavaScript equivalent to PHP’s `sleep()` function?

+ 1,000
Copy linkTweet thisAlerts:
Dec 06.2022

A developer may be working on a website that displays a progress bar while some data is being loaded, and wants to simulate the loading time to give the user the sense of progress.

to post a answer

1 Replies

Copy linkTweet thisAlerts:
@JaySODec 06.2022 — The JavaScript equivalent to PHP's sleep() function is a function that uses the Promise feature to pause the execution of the code for a specified amount of time. Here is an example:

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));

This sleep() function creates a new Promise object and returns it. The Promise constructor takes a function as an argument, which is called the "executor" function. The executor function receives two arguments, resolve and reject, which are both functions. The resolve function is used to indicate that the Promise is fulfilled, and the reject function is used to indicate that the Promise is rejected.

In this case, the sleep() function uses JavaScript's setTimeout() function to schedule a callback that will be executed after the specified delay. The setTimeout() function takes two arguments: a callback function and a delay in milliseconds. The callback function is the resolve function, which is passed to the Promise constructor as an argument. This means that the resolve function will be called after the specified delay, which will fulfill the Promise.

To use the sleep() function, you can call it and await the returned Promise. The await keyword can only be used inside an async function, so you need to wrap the code that calls the sleep() function in an async function.

Here is an example:

async function doSomething() {
await sleep(1000);

In this example, the doSomething() function logs a message to the console, then calls the sleep() function and awaits the returned Promise. This will pause the execution of the doSomething() function for 1 second, after which it will log another message to the console.

Note that the sleep() function in this example is non-blocking, just like PHP's sleep() function. This means that other code can continue to run while the sleep() function is waiting for the specified delay to expire.

If you need to pause the execution of the code and prevent other code from running, you can use the Promise feature in combination with a busy loop. However, this is not recommended, as it can cause the browser to become unresponsive. Instead, you should use the Promise feature to schedule a callback to be executed after a specified delay, without blocking the execution of the rest of the code.


Help @hq spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 7.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ,
analytics: Fullres

legal: ({
terms: of use,
privacy: policy
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
recent_tips: (
tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @Nanditha,
tipped: article
amount: 1000 SATS,