Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么';是否立即清除此设置间隔功能_Javascript_Axios - Fatal编程技术网

Javascript 为什么';是否立即清除此设置间隔功能

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

请参阅下面的代码。预期的行为将是在控制台中看到一个“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

会触发一个或两个以上的请求,因为在响应发生之前,您不会清除它,这大概超过1秒。啊,是的,我没有想到。非常感谢。