gpt4 book ai didi

javascript - 为什么这个setInterval函数没有立即清除

转载 作者:行者123 更新时间:2023-12-03 00:41:46 25 4
gpt4 key购买 nike

请参阅以下代码。预期的行为是在控制台中看到一个“100”,然后当间隔被清除时就什么也没有了。然而,在清除之前,它实际上每次记录 2-3 '100 秒。为什么间隔没有立即清除?

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 会触发一两个以上请求。

关于javascript - 为什么这个setInterval函数没有立即清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53426813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com