调用 clearInterval() 后 setInterval() 循环仍然 运行
setInterval() loop still running after clearInterval() is called
我知道 setInterval() returns 是每个循环的唯一 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() 并且没有抛出任何错误。我做错了什么吗?
1.-
$.get中的数据默认为字符串
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
}
我知道 setInterval() returns 是每个循环的唯一 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() 并且没有抛出任何错误。我做错了什么吗?
1.-
$.get中的数据默认为字符串 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
}