这是我的代码的一部分:
$('#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/