usesetTimeout()
This setTimeout()
function is a built-in JavaScript function that allows you to run a piece of code after a specified amount of time. You can use this to create delays in your scripts. Here is an example:
console.log('Before Sleep');
setTimeout(() => {
console.log('After Sleep');
}, 3000);
In the above example, console.log('Before Sleep')
the statement will be executed immediately, and then there will be a delay of 3 seconds, after which the statement console.log('After Sleep')
will be executed.
useasync/await
Another way to create delays in JavaScript is to useasync/await