簡體   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