HOW TO SET DELAY IN A SET INTERVAL LOOP?

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
 991  View(s)
Ratings:
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!
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to set delay in a set Interval loop?

    Hi Ankita!


    Replace your following line:

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

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

    I hope it resolve your problem!

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!