117

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

Here is my function I want to run:

function FetchData() {
}
1
  • 4
    setTimeout()? setInterval()? Commented Aug 10, 2012 at 11:51

6 Answers 6

214

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.

  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
Sign up to request clarification or add additional context in comments.

1 Comment

Be mindful that the sample does NOT give the most exact answer to the question (it answer the question "how can I call a function every specific interval, not after a specific interval
60

Execute function FetchData() once after 1000 milliseconds:

setTimeout( function() { FetchData(); }, 1000);

Execute function FetchData() repeatedly every 1000 milliseconds:

setInterval( FetchData, 1000);

2 Comments

I guess in case of 'setInterval()' the round brackets for the function name i.e. 'FetchData' should not be present because it triggers the function only once. setInterval(FetchData,1000) seems more valid. I tested it in chrome.
This is incorrect. In the setTimeout the FetchData() will run straight away. You need to wrap it in a function like so : setTimeout(function() { FetchData(); }, 1000);
9

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.

Comments

8

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)

4 Comments

You know what's even cleaner? setTimeout(FetchData, 1000)
Well, if you use any argument inside the FetchData() the script will run right away. so you need to wrap it in the function brackets like I mentioned above.
OP never mentioned any arguments and the function he posted doesn't have any parameters either.
Does not matter, when you learn new things you don't want to sacrafice utility for more clarity, correct?
5

Timeout:

setTimeout(() => {
   console.log('Hello Timeout!')
}, 3000);

Interval:

setInterval(() => {
   console.log('Hello Interval!')
}, 2000);

Comments

0

setTimeout(func, 5000);

-- it will call the function named func() after the time specified. here, 5000 milli seconds , i.e) after 5 seconds

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.