3

I am trying to create a series of clicks on different elements on screen at different times. I can easily do this using the setTimeout function, but I need to make this an infinite loop!?

Here is a snippet of how I am currently handling the code.

setTimeout(function () {jQuery('.CR_1').trigger('click');}, 1000);
setTimeout(function () {jQuery('.CR_1').trigger('click');}, 5000);
setTimeout(function () {jQuery('.CR_2').trigger('click');}, 5500);

Any ideas on how I can make this work?

EDIT: Let me a little more clear. I am trying to run the set of functions in the same order over and over. The setInterval worked perfectly. I am super sorry for any confusion.

   setInterval ( "flips ()", 12000 );


function flips (){
    setTimeout(function () {jQuery('.CR_1').trigger('click');}, 1000);
    setTimeout(function () {jQuery('.CR_1').trigger('click');}, 5000);
    setTimeout(function () {jQuery('.CR_2').trigger('click');}, 5500);

    }
m_gunns
  • 551
  • 1
  • 16
  • 29
  • You can probably find your answer here: http://stackoverflow.com/q/1224463/1031900 – Ofir Farchy Nov 30 '11 at 18:47
  • It's not entirely clear what you're trying to do. I've assumed you want to do one click at a time, some others have assumed you want independent timers running all over the place... :-) – T.J. Crowder Nov 30 '11 at 18:48
  • Note that you should change your code to just `setInterval(flips,12000)`; no reason to use the string form (which creates a function wrapper around your code). – Phrogz Nov 30 '11 at 20:54

5 Answers5

3

Just call setTimeout from within your function.

setTimeout(callMe, 1000);

function callMe() {
    jQuery('.CR_1').trigger('click');
    setTimeout(callMe, 1000);
}

You could also use setInterval but I prefer doing it this way because it will be called 1000ms from the last run, not every 1000ms regardless of how long it takes to run (if the process is synchronous).

BNL
  • 7,085
  • 4
  • 27
  • 32
3
clicky()

function clicky() {
   setTimeout(function () {jQuery('.CR_1').trigger('click');}, 1000);
   setTimeout(function () {jQuery('.CR_1').trigger('click');}, 5000);
   setTimeout(function () {jQuery('.CR_2').trigger('click');clicky()}, 5500);
}
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
1

Why not use setInterval instead?

Darek Rossman
  • 1,323
  • 1
  • 10
  • 12
1

i think you should use setInterval instead of setTimeout

MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
1
var delayedFunctions = [
  [1000,function(){ ... }],
  [5000,function(){ ... }],
  [5500,function(){ ... }]
];

var fIndex = 0;
function runDelayedFunctions(){
  var details = delayedFunctions[fIndex];
  setTimeout( function(){
    details[1].call(this);
    if (++fIndex >= delayedFunctions.length) fIndex=0;
    runDelayedFunctions();
  }, details[0] );
};
runDelayedFunctions();
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • Why make something simple so complicated? – BNL Nov 30 '11 at 18:52
  • @BNL a) It's not that complicated, IMO; b) based on my understanding (and TJCrowder's) of the original question, this is one of the simpler ways to accomplish it **correctly**. Running _n_ concurrent `setInterval` loops (as your answer basically does) is fragile. For example, if the first function consistently runs longer than the others, your answer will eventually break, with the calling order getting thrown out of whack. – Phrogz Nov 30 '11 at 20:58