In JavaScript, How can I call a function after a specific time interval?
Here is my function I want to run:
function FetchData() {
}
This question is related to
javascript
function
timer
setTimeout(func, 5000);
-- it will call the function named func() after the time specified. here, 5000 milli seconds , i.e) after 5 seconds
setTimeout(() => {
console.log('Hello Timeout!')
}, 3000);
setInterval(() => {
console.log('Hello Interval!')
}, 2000);
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.
ECMAScript 6 introduced arrow functions so now the setTimeout() or setInterval() don't have to look like this:
setTimeout(function() { FetchData(); }, 1000)
Instead, you can use annonymous arrow function which looks cleaner, and less confusing:
setTimeout(() => {FetchData();}, 1000)
Execute function FetchData()
once after 1000 milliseconds:
setTimeout(FetchData,1000);
Execute function FetchData()
repeatedly every 1000 milliseconds:
setInterval(FetchData,1000);
Source: Stackoverflow.com