Alex Rivera | Logout

Is there any way to call a function periodically in JavaScript?

Asked 2009-08-03T20:31:23.027
181

Is there any way to call a function periodically in JavaScript?

Edit
Report

1 Answer

276

The setInterval() method, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. It returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval().

var intervalId = setInterval(function() {
  alert("Interval reached every 5s")
}, 5000);

// You can clear a periodic function by uncommenting:
// clearInterval(intervalId);

See more @ setInterval() @ MDN Web Docs

answered 2009-08-03T20:35:50.600

Your Answer