我知道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);
我在调试中检查了这个,并且肯定会调用clearInterval()并且不会抛出任何错误。我做了一件明显不对的事吗?
答案 0 :(得分:2)
1 .-
$ .get中的数据默认为String https://api.jquery.com/jquery.get/
data = parseInt(data)
2.-
您添加了第二级同步...您确定此请求的速度超过了3000毫秒吗?
3.-如果数据是> 99,代码有效。
在我关注的问题中,问题是请求超过3秒,您仍然会收到之前发布的连接。
if (data > 99) {
clearInterval(refresh);
$("#recalculation-message").text("Recalculation complete.");
} else {
//relaunch here the connection and remove the interval
}