How to Wait 1 Second in JavaScript
Aug 27, 2021
To delay a function execution in JavaScript by 1 second, wrap a promise execution inside a function and wrap the Promise's resolve()
in a setTimeout()
as shown below.
setTimeout()
accepts time in milliseconds, so setTimeout(fn, 1000)
tells JavaScript to call fn
after 1 second.
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
delay(1000).then(() => console.log('ran after 1 second1 passed'));
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));
}
async function test() {
console.log('start timer');
await delay(1000);
console.log('after 1 second');
}
test();
You may also skip the extra delay()
function and just inline the Promise constructor call as shown below.
async function test() {
console.log('start timer');
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('after 1 second');
}
test();
Did you find this tutorial useful? Say thanks by starring our repo on GitHub!