Javascript 为什么';是否立即清除此设置间隔功能
请参阅下面的代码。预期的行为将是在控制台中看到一个“100”,然后随着时间间隔的清除,什么也看不到。然而,在每次清除之前,它实际上记录在2-3'100之间。为什么不能立即清除间隔Javascript 为什么';是否立即清除此设置间隔功能,javascript,axios,Javascript,Axios,请参阅下面的代码。预期的行为将是在控制台中看到一个“100”,然后随着时间间隔的清除,什么也看不到。然而,在每次清除之前,它实际上记录在2-3'100之间。为什么不能立即清除间隔 var endpoint = "https://example.com"; axios .post() .then(function(response) { var timerId = setInterval(function() { axios .get(endpoint
var endpoint = "https://example.com";
axios
.post()
.then(function(response) {
var timerId = setInterval(function() {
axios
.get(endpoint + response.data.url)
.then(function(response) {
console.log(response.data.percentageComplete);
if (response.data.percentageComplete == 100) clearInterval(timerId);
})
.catch(function(error) {});
}, 1000);
})
.catch(function(error) {});
因为您将在HTTP请求成功完成后清除它,而该请求可能需要几秒钟才能完成。同时,
setInterval
会触发一个或两个以上的请求,因为在响应发生之前,您不会清除它,这大概超过1秒。啊,是的,我没有想到。非常感谢。