我花了很多时间来清理一个非常简单的间隔。有时有效,有时无效。
我通过以下方式设置首次启动 Bootstrap 模式时的间隔:
$('#myModal').on('shown.bs.modal', function (e) {
getParticipants();
getParticipantsInt = setInterval(getParticipants, 15000);
});
然后,当用户确认(通过另一个模式)他们想要删除用户时,我尝试清除间隔:
$("#deleteBtn").click(function() {
clearInterval( getParticipantsInt );
$(parent).closest('tr').remove(); // Remove the row
test.dropUser(userId)
.success(function(response){
$('#hangupPartModal').modal('hide');
setTimeout( function()
{
getParticipantsInt = setInterval(getMtgParticipants, 15000);
}, 5000);
});
});
我想要的行为是,一旦显示模式,就调用该方法,然后设置每 15 秒调用该方法的时间间隔。
当我们删除用户时,取消间隔,基本上再等待 20 秒才能重新启动。
问题是有时在 20 秒结束之前它仍然被调用。另外,在删除用户之前,有时我可以连续看到 getParticipants() 方法 2 或 3 次。
我在这里缺少什么?设置/清除间隔应该很容易。
最佳答案
$('#myModal').on('shown.bs.modal', function (e) {
getParticipants();
clearInterval(getParticipantsint);
getParticipantsInt = setInterval(getParticipants, 15000);
});
我假设 getParticipantsInt 是全局的。如果这是真的,问题可能是您有多个时间间隔,但只清除了其中一个。
关于javascript - 间隔并不总是被清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29239564/