在调用clearInterval()之后,setInterval()循环仍在运行 [英] setInterval() loop still running after clearInterval() is called
问题描述
我知道setInterval()在每个循环上都会返回一个唯一的ID,并且必须使用该ID调用clearInterval才能杀死该循环.我相信我的代码可以做到这一点,但是循环仍会无限期地运行.
I know that setInterval() returns a unique ID on every loop, and that clearInterval must be called with that ID to kill the loop. I believe my code does this, yet the loop continues to run indefinitely.
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);
我已经在debug中检查了这一点,并且明确调用了clearInterval(),并且不会引发任何错误.我做错了什么吗?
I have checked this in debug and clearInterval() is definitely being called and no errors are thrown. Am I doing something obviously wrong?
推荐答案
1 .-
数据默认为字符串 https://api.jquery.com/jquery.get/
data = parseInt(data)
2.-
您添加了第二级同步...您确定此请求的速度快于3000毫秒吗?
You add a second level of sync... Are you sure that this request is faster than 3000ms?
3.-如果数据> 99,则代码有效.
3.- If the data is > 99, the code works.
我担心的是,该请求的时间超过了3秒,并且您仍然收到以前启动的连接.
In my concern, the problem is the request is longer than 3 seconds, and you still receive connections that your previously launched.
if (data > 99) {
clearInterval(refresh);
$("#recalculation-message").text("Recalculation complete.");
} else {
//relaunch here the connection and remove the interval
}
这篇关于在调用clearInterval()之后,setInterval()循环仍在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!