Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调用clearInterval()后,setInterval()循环仍在运行_Javascript_Jquery_Setinterval_Clearinterval - Fatal编程技术网

Javascript 调用clearInterval()后,setInterval()循环仍在运行

Javascript 调用clearInterval()后,setInterval()循环仍在运行,javascript,jquery,setinterval,clearinterval,Javascript,Jquery,Setinterval,Clearinterval,我知道setInterval()在每个循环上都返回一个唯一的ID,必须使用该ID调用clearInterval才能终止循环。我相信我的代码可以做到这一点,但循环仍然无限期地运行 var refresh = setInterval(function () { $.get(url, function (data) { success: { if (data < 5) {

我知道setInterval()在每个循环上都返回一个唯一的ID,必须使用该ID调用clearInterval才能终止循环。我相信我的代码可以做到这一点,但循环仍然无限期地运行

var refresh = setInterval(function () {
            $.get(url, function (data) {
                success: {
                    if (data < 5) {
                        data = 5;
                    }
                    var width = data + "%";
                    $("#recalculation-progress-bar").css('width', width);

                    if (data > 99) {
                        clearInterval(refresh);
                        $("#recalculation-message").text("Recalculation complete.");
                    }
                }
            });

        }, 3000);
var refresh=setInterval(函数(){
$.get(url、函数(数据){
成功:{
如果(数据<5){
数据=5;
}
变量宽度=数据+“%”;
$(“#重新计算进度条”).css('width',width);
如果(数据>99){
清除间隔(刷新);
$(“#重新计算消息”).text(“重新计算完成”);
}
}
});
}, 3000);
我在debug中检查了这一点,并且clearInterval()肯定会被调用,并且不会抛出任何错误。我是不是做错了什么-

默认情况下,$.get中的数据是字符串

2.-

您添加了第二级同步。。。您确定此请求的速度超过3000毫秒吗

3.-如果数据大于99,则代码有效

在我看来,问题在于请求时间超过3秒,并且您仍然会收到以前启动的连接

    if (data > 99) {
        clearInterval(refresh);
        $("#recalculation-message").text("Recalculation complete.");
    } else {
    //relaunch here the connection and remove the interval
    }

“我是否做了一些明显错误的事情?”不是在该代码中,不是。我的猜测是,您最终调用它(至少)两次,因此第二次会覆盖
刷新中的值,因此您只会取消第二次(或第n次),而不是全部。也就是说,将这样的两个异步活动组合在一起总是很危险的
before get和before clearInterval来双重检查它们是否是相同的值。@Khalid
success:{}
是一个
标签
。检查这个@AhmadIbrahim,谢谢我刚刚忘记javascript标签!!谢谢你提醒我!这是一个很好的观点-请求的长度超过3000毫秒。我将尝试更快的请求,看看问题是否仍然存在。这里运气好吗?对不起,我忘记将此标记为已回答!最后我做了一些稍有不同的事情,但基于你的回答。谢谢你的回复。
    if (data > 99) {
        clearInterval(refresh);
        $("#recalculation-message").text("Recalculation complete.");
    } else {
    //relaunch here the connection and remove the interval
    }