还有另一个 javascript clearInterval 不起作用 [英] And yet another javascript clearInterval not working

查看:42
本文介绍了还有另一个 javascript clearInterval 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过很多这样的话题并浏览了其中的一些,但到目前为止似乎没有任何帮助.所以我试图在 ajax 调用发生时连续调用计时器.一旦 ajax 调用到达 complete 事件,我想清除计时器,但这似乎不起作用,因为对 CheckProgress() 的调用不断进行.

I've seen a bunch of these threads and went through a few of them but nothing seemed to be of help so far. So I'm trying to call the timer continuously while the ajax call is taking place. Once the ajax call reaches the complete event, I'd like to clearInterval the timer, but that doesn't seem to be working because the call to CheckProgress() keeps going and going.

这是我的代码:

var timer = "";

        $("#ChartUpdateData").click(function () {
            $("#loadingimgfeatdiv").show(); //ajax loading gif
            if (timer == "")
            {
                console.log("Starting Progress Checks...");
                timer = window.setInterval("CheckProgress()", 5000);
            }

            $.ajax({
                type: "POST",
                async: true,
                url: '@(Url.Action("UpdateServerData","Charts"))',
                contentType: "application/json; charset=utf-8",
                success: function (data) {

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {

                },
                complete:function (jqXHR, textStatus) {
                    $("#loadingimgfeatdiv").hide();
                    StopCheckingProgress();
                    LoadChart();
                },
            });

        });

    function StopCheckingProgress()
    {
        window.clearInterval(timer);
        timer = "";
        console.log("Ending Progress Checks...");
    }
    function CheckProgress()
    {
        console.log("Checking Progress...");
        console.log(timer);
    }

推荐答案

我从不喜欢 setInterval.我喜欢直接管理计时器.

I've never liked setInterval. I like managing timers directly.

var timerStop = false

$("#ChartUpdateData").click(function () {
    $("#loadingimgfeatdiv").show(); //ajax loading gif
    if (!timerStop) {
        console.log("Starting Progress Checks...");
        CheckProgress()
    }

    $.ajax({
        type: "POST",
        async: true,
        url: '@(Url.Action("UpdateServerData","Charts"))',
        contentType: "application/json; charset=utf-8",
        success: function (data) {

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {

        },
        complete:function (jqXHR, textStatus) {
            $("#loadingimgfeatdiv").hide();
            timerStop = true;
        },
    });

});

function CheckProgress()  {
    if(timerStop) {
        console.log("Ending Progress Checks...");
        return;
    }
    console.log("Checking Progress...");
    console.log(timer);
    window.setTimeout(function(){CheckProgress()}, 5000);
}

这篇关于还有另一个 javascript clearInterval 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆