Categories
JavaScript Answers

How to wait for a period of time after a function run with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *