Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery/JS infinite timed loop

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);

    }
like image 596
m_gunns Avatar asked Sep 10 '26 00:09

m_gunns


1 Answers

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).

like image 192
BNL Avatar answered Sep 12 '26 15:09

BNL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!