How to Wait 1 Second in JavaScript - Mastering JS
How to Wait 1 Second in JavaScript - Mastering JS
Mastering JS ☰
Tutorials / Fundamentals /
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
You could also wrap the delay call in an async function to use async await instead of
then() :
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
test();
You may also skip the extra delay() function and just inline the Promise constructor
call as shown below.
test();
https://masteringjs.io/tutorials/fundamentals/wait-1-second-then 1/3
12/12/2024, 10:45 How to Wait 1 Second in JavaScript - Mastering JS
Did you find this tutorial useful? Say thanks by starring our repo on GitHub!
Star 150
https://masteringjs.io/tutorials/fundamentals/wait-1-second-then 2/3
12/12/2024, 10:45 How to Wait 1 Second in JavaScript - Mastering JS
https://masteringjs.io/tutorials/fundamentals/wait-1-second-then 3/3