Creative Assets & Unlimited Downloads on Envato Elements
In the example above, the
console.log('Before Sleep') statement will be executed immediately, followed by a 3-second delay, after which the
console.log('After Sleep') statement will be executed.
async/await syntax. With
async/await, you can write asynchronous code that runs in a way that is similar to synchronous code. For example:
In this example, we’ve created an
async function called
sleep that returns a
Promise that resolves after a specified amount of time. The main function uses the
await keyword to wait for the sleep function to complete, effectively creating a 3-second delay.
async/await is only supported in modern browsers and requires a runtime with support for Promises.
Both of these methods can be used to achieve a similar result to the PHP
sleep() function, but with different syntax and limitations. Choose the method that works best for your use case.
This post may contain affiliate links. See our disclosure about affiliate links here.
Subscribe to our popular newsletter and get the latest web design news and resources directly in your inbox.
© Copyright 2023 1stWebDesigner
Helping You Build a Better Web