forum

Home / DeveloperSection / Forums / How to set delay in a set Interval loop?

How to set delay in a set Interval loop?

Anonymous User 1858 03-May-2013
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!

Updated on 03-May-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By