javascript - How to set delay in a setInterval loop? -


here part of codes:

$('#monitor').click(function(){             setinterval(function(){                     $('#status_table tr [id^="monitor_"]:checked').each(function () {                         monitoring($(this).parents('tr'));                      });                 },15000);         }); 

i want call function monitoring each row in table has checkbox checked. if have one, working fine. when have more one, jumbles up, means not append correct status in table. here function monitoring:

       function monitoring($row) {             fbtype = $row.find('td:nth-child(3)').html();             fbnum = $row.find('td:nth-child(4)').html();             eachstatus =$row.find('td:nth-child(5)').attr('id');             $('#ptest').append(fbtype + ' '+ fbnum+' '+ eachstatus +'<br>');              $.post('/request', {inputtext: fbnum,key_pressed: fbtype.tostring()}).done(function (reply) {                 if (reply == "on") {                     $('#status_table tr #'+eachstatus).empty().append("on");                  } else if (reply =="off") {                     $('#status_table tr #'+eachstatus).empty().append("off");                  }             });         } 

how can delay function call each row? tried following

       $('#monitor').click(function(){             setinterval(function(){                     $('#status_table tr [id^="monitor_"]:checked').each(function () {                        settimeout(function(){                             monitoring($(this).parents('tr'));                        });                      },1000);                 },15000);         }); 

but div #ptest displays undefined.

you delaying them together, that's why still run together. want delay them from each other.

$('#monitor').click(function(){    //a jquery object array   var checked = $('#status_table tr [id^="monitor_"]:checked');    (function loop(i){      //monitor element @ index     monitoring($(checked[i]).parent('tr'));      //delay     settimeout(function(){       //when incremented less number of rows, call loop next index       if(++i<checked.length) loop(i);     },15000);    }(0)); //start 0  }); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -