JavaScript loop timer

I need to execute a piece of JavaScript code, say, every 2000 milliseconds.

setTimeout('moveItem()',2000) 

The above function will be executed after 2000 milliseconds, but will not perform it again.

So, inside my moveItem function, I have:

 function moveItem() { jQuery(".stripTransmitter ul li a").trigger('click'); setInterval('moverItem()',2000); } 

This does not work because I want to execute the trigger by clicking on a piece of jQuery code every interval of 2000 milliseconds, but now it is being called all the time and the script should be interrupted. Also, I feel this is a very bad coding ... How would you guys solve this?

+63
javascript jquery
Jan 25 2018-10-25T00:
source share
6 answers

Note that setTimeout and setInterval are very different functions:

  • setTimeout will execute the code once, after a timeout.
  • setInterval will execute the code forever, with an interval of the timeout provided.

Both functions return a timer identifier that you can use to cancel the timeout. All you have to do is save this value in a variable and use it as an argument to clearTimeout(tid) or clearInterval(tid) respectively.

So, depending on what you want to do, you have two valid options:

 // set timeout var tid = setTimeout(mycode, 2000); function mycode() { // do some stuff... tid = setTimeout(mycode, 2000); // repeat myself } function abortTimer() { // to be called when you want to stop the timer clearTimeout(tid); } 

or

 // set interval var tid = setInterval(mycode, 2000); function mycode() { // do some stuff... // no need to recall the function (it an interval, it'll loop forever) } function abortTimer() { // to be called when you want to stop the timer clearInterval(tid); } 

Both are very common ways to achieve the same.

+174
Jan 25 '10 at 3:18
source share
 setInterval(moveItem, 2000); 

is a way to execute the moveItem function every 2 seconds. The main problem with your code is that you are calling setInterval internally and not outside of the callback. If I understand what you are trying to do, you can use this:

 function moveItem() { jQuery('.stripTransmitter ul li a').trigger('click'); } setInterval(moveItem, 2000); 

NB: Do not pass strings to setTimeout or setInterval - it is best to use an anonymous function or function identifier (as I said above). Also, be careful not to mix single and double quotes. Choose one and stick to it.

+6
Jan 25 '10 at 15:17
source share

I believe you are looking for setInterval ()

+2
Jan 25 '10 at 15:17
source share

It should be:

 function moveItem() { jQuery(".stripTransmitter ul li a").trigger('click'); } setInterval(moveItem,2000); 

setInterval(f, t) calls the argument function, f , once every t milliseconds.

+2
Jan 25 '10 at 15:17
source share

It uses an automatic loop function with html code. Hope this can be helpful for someone.

 <!DOCTYPE html> <html> <head> <style> div { position: relative; background-color: #abc; width: 40px; height: 40px; float: left; margin: 5px; } </style> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <p><button id="go">Run ยป</button></p> <div class="block"></div> <script> function test() { $(".block").animate({left: "+=50", opacity: 1}, 500 ); setTimeout(mycode, 2000); }; $( "#go" ).click(function(){ test(); }); </script> </body> </html> 

Fiddle: DEMO

0
Feb 19 '13 at 13:00
source share

You should try something like this:

  function update(){ i++; document.getElementById('tekst').innerHTML = i; setInterval(update(),1000); } 

This means that you need to create a function in which you do what you need to do, and make sure that it will call itself at the interval you need. In your onload body, call the function for the first time as follows:

 <body onload="update()"> 
-3
Jan 25 '10 at 15:17
source share



All Articles