繁体   English   中英

为什么不立即清除此setInterval函数

[英]Why doesn't this setInterval function clear immediately

请参阅以下代码。 预期的行为是在控制台中看到单个“ 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触发其中一个或两个以上请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM