Bounties /Pin to ProfileBookmark

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

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
December 6th, 2022JavaScript

1 Answer(s)

answer
@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() {
console.log("Starting...");
await sleep(1000);
console.log("Done!");
}


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.
×

Success!

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

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with Github
about: ({
version: 0.1.7 BETA 2.4,
whats_new: article editor v2,
up_next: tip code demos,
coming_soon: embeddable tipping,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh

version: 0.1.5,
notes: added pins + new badge

version: 0.1.4,
notes: home refresh + tools
)...
recent_tips: (
tipper: @JohnDavis,
amount: 250 SATS,
source: article

tipper: @Megan0910,
amount: 1000 SATS,
source: article

tipper: @JohnDavis,
amount: 250 SATS,
source: article
)...