How to repeat code every 4 seconds with JavaScript?

Sometimes, we want to repeat code every 4 seconds with JavaScript.

In this article, we’ll look at how to repeat code every 4 seconds with JavaScript.

How to repeat code every 4 seconds with JavaScript?

To repeat code every 4 seconds with JavaScript, we can call setInterval.

For instance, we write:

const fn = () => {
  console.log('hello')
}
setInterval(fn, 4000)

to call setInterval with fn and 4000 to run fn every 4 seconds.

As a result, 'hello' is logged every 4 seconds.

Conclusion

To repeat code every 4 seconds with JavaScript, we can call setInterval.