javascript - 如何在 setInterval 循环中设置延迟?

标签 javascript jquery

这是我的代码的一部分:

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

我想为表中选中复选框的每一行调用函数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");

                }
            });
        }

如何延迟每行的函数调用?我尝试了以下方法

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

但是 div #ptest 显示未定义

最佳答案

替换以下行:

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

对于这个:

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

关于javascript - 如何在 setInterval 循环中设置延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16353731/

相关文章:

javascript - 使相同事件的对象方法代码变得干燥

有关未定义或 null 引用的 JavaScript 运行时错误

javascript - 在渲染模板中创建按钮

php - 本地化:子域、目录或域

javascript - 获取元素编号或为其设置标识符

jquery - 如何在动画时查看元素的宽度?

javascript - 我想在 Node.js 和 EJS 中使用 JQuery 但 $ 未定义

javascript - 为什么一个 ko.mapping.fromJS 工作而另一个不工作?

javascript - 在 WordPress 循环中使用 Jquery .next

javascript - 如何从矩阵SVG中提取位置、旋转和缩放