Sometimes, we want to wait for a period of time after a function run with JavaScript.
In this article, we’ll look at how to wait for a period of time after a function run with JavaScript.
How to wait for a period of time after a function run with JavaScript?
To wait for a period of time after a function run with JavaScript, we can use the setTimeout
function.
For instance, we write:
setTimeout(() => {
console.log('hello')
}, 2000);
to call setTimeout
with a callback that logs 'hello'
in 2000 milliseconds.
Therefore, we see 'hello'
logged in 2000 milliseconds after running this code.
Conclusion
To wait for a period of time after a function run with JavaScript, we can use the setTimeout
function.