184

Possible Duplicate:
Calling a function every 60 seconds

I want to Call a Javascript function every 5 seconds continuously. I have seen the setTimeOut event. Will it be working fine if I want it continuously?

Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
deepmoteria
  • 2,279
  • 2
  • 19
  • 20
  • 1
    duplicates http://stackoverflow.com/questions/2170923/whats-the-easiest-way-to-call-a-function-every-5-seconds-in-jquery – milkovsky Jun 28 '16 at 13:44

5 Answers5

304

You can use setInterval(), the arguments are the same.

const interval = setInterval(function() {
   // method to be executed;
 }, 5000);

clearInterval(interval); // thanks @Luca D'Amico
srk
  • 1,625
  • 1
  • 10
  • 26
Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35
  • http://blogs.msdn.com/b/ericlippert/archive/2003/11/01/53329.aspx – Jonas Høgh Aug 25 '11 at 09:46
  • 1
    @Anantha: FYI, The code you had before your latest edit (after my edit) (`setInterval(methodName,5000)`), did not use `eval`. `eval` is only used in `setInterval`/`setTimeout` if the first parameter is passed as a string. – Matt Aug 25 '11 at 14:02
  • As a side note: if you want to stop your Interval, you should call `clearInterval()` passing your Interval ID as a parameter. – Luca D'Amico Jul 25 '19 at 16:22
  • slightly improved: function anyActivity(){ //do something. } const interval = setInterval(anyActivity, 5000); window.onbeforeunload = function(){ clearInterval(interval); } $(document).ready(function() { anyActivity(); }); – Bharat Darakh Feb 16 '22 at 05:12
137

Do a "recursive" setTimeout of your function, and it will keep being executed every amount of time defined:

function yourFunction(){
    // do whatever you like here

    setTimeout(yourFunction, 5000);
}

yourFunction();
Shef
  • 44,808
  • 15
  • 79
  • 90
  • 7
    I'm curious, won't this cause a memory leak eventually? – basickarl Sep 25 '15 at 15:32
  • 23
    @KarlMorrison No, using `setInterval` can cause a memory leak. By using `setTimeout` you ensure that the next function call won't get triggered **until** the previous function call has finished. – gion_13 Nov 16 '15 at 07:10
  • 2
    How do we break out of this? – Saras Arya Mar 31 '16 at 08:45
  • 2
    @SarasArya Easily, you can have a variable outside the scope of the function which holds a state. Then, before setting the timeout inside the function again, you do an IF check. So, according to the logic you need, you set the timeout or not as needed. – Shef Mar 31 '16 at 08:48
  • 1
    @Shef Thanks... Geez that was easy. :P – Saras Arya Mar 31 '16 at 08:50
  • I had to add `() =>` as the function was executing in a loop. `setTimeout(() => yourFunction, 5000);` – Defozo Aug 13 '19 at 13:43
  • @Defozo That doesn't seem right. Were you using `yourFunction()` instead, maybe? – John Montgomery Sep 11 '19 at 17:23
56

As best coding practices suggests, use setTimeout instead of setInterval.

function foo() {

    // your function code here

    setTimeout(foo, 5000);
}

foo();

Please note that this is NOT a recursive function. The function is not calling itself before it ends, it's calling a setTimeout function that will be later call the same function again.

Andre Miras
  • 3,580
  • 44
  • 47
Jose Faeti
  • 12,126
  • 5
  • 38
  • 52
  • 3
    This also executes the code immediately the first time, while setInterval waits first. – William Randokun Apr 12 '19 at 10:30
  • 1
    How do I make this function stop? Also, I'm getting a stack overflow. – WarpPrime Jan 03 '21 at 00:45
  • 1
    setTimeout returns a timeoutID you can then pass to clearTimeout to stop it for good. You won't get a stack overflow if you follow the example of the answer. You are likely calling the function recursively, instead of letting the setTimeout method call it – Jose Faeti Jan 03 '21 at 19:18
  • Edit queue is full, just wanted to say that this will call the function every **5 minutes**, not **5 seconds**. – ThatBirdThatLearnedToCode Mar 29 '22 at 13:13
  • @ThatBirdThatLearnedToCode setTimeout second argument is in milliseconds. 1000 ms is 1 second, so 5000 is 5 seconds. See https://developer.mozilla.org/en-US/docs/Web/API/setTimeout – Jose Faeti Mar 30 '22 at 11:42
  • @Jose Faeti lol I am an idiot, I forgot how to read milliseconds (I knew it was milliseconds) – ThatBirdThatLearnedToCode Mar 31 '22 at 16:57
26

For repeating an action in the future, there is the built in setInterval function that you can use instead of setTimeout.
It has a similar signature, so the transition from one to another is simple:

setInterval(function() {
    // do stuff
}, duration);
gion_13
  • 41,171
  • 10
  • 96
  • 108
6

Good working example here: http://jsfiddle.net/MrTest/t4NXD/62/

Plus:

  • has nice fade in / fade out animation
  • will pause on :hover
  • will prevent running multiple actions (finish run animation before starting second)
  • will prevent going broken when in the tab ( browser stops scripts in the tabs)

Tested and working!

Iladarsda
  • 10,640
  • 39
  • 106
  • 170