Javascript: Call a function after specific time period

Shaggy picture Shaggy · Aug 10, 2012 · Viewed 147.8k times · Source

In JavaScript, How can I call a function after a specific time interval?

Here is my function I want to run:

function FetchData() {
}

Answer

Akash KC picture Akash KC · Aug 10, 2012

You can use JavaScript Timing Events to call function after certain interval of time:

This shows the alert box after 3 seconds:

setInterval(function(){alert("Hello")},3000);

You can use two method of time event in javascript.i.e.

  1. setInterval(): executes a function, over and over again, at specified time intervals
  2. setTimeout() : executes a function, once, after waiting a specified number of milliseconds