Javascript: Call a function after specific time period
In JavaScript, How can I call a function after a specific time interval?
Here is my function I want to run:
function FetchData() {
}
Solution 1:
You can use JavaScript Timing Events to call function after certain interval of time:
This shows the alert box every 3 seconds:
setInterval(function(){alert("Hello")},3000);
You can use two method of time event in javascript.i.e.
-
setInterval()
: executes a function, over and over again, at specified time intervals -
setTimeout()
: executes a function, once, after waiting a specified number of milliseconds
Solution 2:
Execute function FetchData()
once after 1000 milliseconds:
setTimeout( function() { FetchData(); }, 1000);
Execute function FetchData()
repeatedly every 1000 milliseconds:
setInterval( FetchData, 1000);
Solution 3:
sounds like you're looking for setInterval. It's as easy as this:
function FetchData() {
// do something
}
setInterval(FetchData, 60000);
if you only want to call something once, theres setTimeout.