And yet another javascript clearInterval not working

坚强是说给别人听的谎言 提交于 2019-12-13 02:11:48

问题


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.

Here's my code:

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);
    }

EDIT:


回答1:


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);
}



回答2:


Your code is fine. Here is a fiddle. It works on Google Chrome and Firefox just as you expected. Can you confirm this snippet is not working on your machine?

I made few tiny changes:

  • AJAX call to /echo/json
  • Smaller interval (50 ms)
  • function reference: setInterval(CheckProgress, 5000) instead of string with JavaScript

Interval function is called few times and is cleared once echo service AJAX call returns. Exactly how you want it to be.

Can you reproduce the problem there?



来源:https://stackoverflow.com/questions/7560121/and-yet-another-javascript-clearinterval-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!