Home > DeveloperSection > Forums > How to set delay in a set Interval loop?
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    May-03-2013 1:15 AM

 JavaScript JavaScript 
Ratings:


 1 Reply(s)
 677  View(s)
Rate this:
Hi Everyone!

Here is part of my codes:

$('#monitor').click(function(){
            setInterval(function(){
                    $('#status_table tr [id^="monitor_"]:checked').each(function () {
                        monitoring($(this).parents('tr'));
                     });
                },15000);
        });
I want to call the function monitoring for each row in a table that has its checkbox checked. if i only have one, it is working fine. But when i have more than one, 

it jumbles up, which means it will not append the correct status in the table. Here is my 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 i delay the function call for each row? i tried the following

       $('#monitor').click(function(){
            setInterval(function(){
                    $('#status_table tr [id^="monitor_"]:checked').each(function () {
                       setTimeout(function(){
                            monitoring($(this).parents('tr'));
                       });
                     });
                },15000);
        });
But the div #ptest displays undefined.

Please help me!


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-03-2013 9:06 AM

Hi Ankita!

Replace your following line:

monitoring($(this).parents('tr'));
for this one:

monitoring($(this).parent('tr'));

I hope it resolve your problem!

Don't want to miss updates? Please click the below button!

Follow MindStick