javascript - 间隔并不总是被清除

标签 javascript jquery html twitter-bootstrap

我花了很多时间来清理一个非常简单的间隔。有时有效,有时无效。

我通过以下方式设置首次启动 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/

相关文章:

javascript - 如何创建 JavaScript 库

html - 在 wordpress 中粘贴引导菜单

python - 使用 python 从 THE(西类牙电视网站)下载视频

javascript - 当我尝试在循环内使用 .push 时,它似乎在间隔号之前开始一个阶段,这似乎会导致溢出?

html - 如何让右边的元素保持在同一行,而左边的元素换行?

javascript - 使用 jqplot LineChart 创建空图表

javascript - 命名;在javascript中动态创建变量

javascript - 从文件 : Javascript? 读取字符串

jquery - 自动调整大小在 primefaces 5.3.6 的输入文本区域不起作用

javascript - Angularjs - 单个指令宽度