Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.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 jQuery设置间隔问题_Javascript_Jquery - Fatal编程技术网

Javascript jQuery设置间隔问题

Javascript jQuery设置间隔问题,javascript,jquery,Javascript,Jquery,我正在运行一个函数,它每秒检查一个远程脚本并更新一个进度条。我是新来的setInterval,在完成后让它停止循环时遇到了问题 我添加了var intervalpost函数和clearInterval(interval)在几个不同的地方-基本上像是遮住眼睛和把尾巴钉在驴子上-都没有成功。在哪里设置clearInterval以在data达到100时停止循环 function getProgress(hash) { setInterval(function() { $.ajax({

我正在运行一个函数,它每秒检查一个远程脚本并更新一个进度条。我是新来的
setInterval
,在完成后让它停止循环时遇到了问题

我添加了
var interval
post函数和
clearInterval(interval)在几个不同的地方-基本上像是遮住眼睛和把尾巴钉在驴子上-都没有成功。在哪里设置
clearInterval
以在
data
达到100时停止循环

function getProgress(hash) {
  setInterval(function() {
    $.ajax({
        type: 'GET',
        url: 'http://domain.com/script.php?hash=' + hash,
        cache: false,
        dataType: 'json',
        success: function(data) {
          if (data < 100) {
            getProgress();
          } else {
            $('#progress-bar').hide();
          }
        },
    });
  }, 1000);
}
函数getProgress(散列){ setInterval(函数(){ $.ajax({ 键入:“GET”, 网址:'http://domain.com/script.php?hash=“+hash, cache:false, 数据类型:“json”, 成功:功能(数据){ 如果(数据<100){ getProgress(); }否则{ $(“#进度条”).hide(); } }, }); }, 1000); }
我假设您的代码在隐藏进度条时完成。您需要将间隔设置为某个变量,可以使用该变量清除间隔

function getProgress(hash) {
  var someVariable = setInterval(function() {
    $.ajax({
        type: 'GET',
        url: 'http://domain.com/script.php?hash=' + hash,
        cache: false,
        dataType: 'json',
        success: function(data) {
          if (data < 100) {
            getProgress();
          } else {
            $('#progress-bar').hide();
            clearInterval(someVariable);
          }
        },
    });
  }, 1000);
}
函数getProgress(散列){ var someVariable=setInterval(函数(){ $.ajax({ 键入:“GET”, 网址:'http://domain.com/script.php?hash=“+hash, cache:false, 数据类型:“json”, 成功:功能(数据){ 如果(数据<100){ getProgress(); }否则{ $(“#进度条”).hide(); clearInterval(某些变量); } }, }); }, 1000); } 好例子:

调用setInterval时,需要将返回值设置为variable。
然后您可以使用此参数调用clearInterval,它将停止。

这是假设您无法停止以前的间隔的答案。每次
数据<100
时,您都在调用
getProgress
方法。它调用另一个
setInterval
方法,并为该间隔重新创建一个新的
id

假设
getProgress
方法在循环中调用了不止一次

var intervalId;
function getProgress(hash) {
  intervalId = setInterval(function() {..}, 1000);
}
下次您想要停止间隔时,可以调用
clearInterval(intervalId)
。这是非常错误的。您有多个1间隔,而您恰好停止了上一个间隔,而上一个间隔仍在运行,直到
data<100
false

我建议将间隔ID存储在数组中,如

var intervalIds = [];
function getProgress(hash) {
  intervalId = setInterval(function() {..}, 1000);
  intervalIds.push(intervalId);
}
如果下次你想停止间歇,你可以这样做

for (var idx in intervalIds) {
   clearInterval(intervalIds[idx]);
}
但是,这只是停止所有间歇的一种方法。我想说的是你的代码很难看。如果使用
setInterval
方法的原因是等待
1000 ms
检索另一个数据,
setTimeout
可以很好地完成这项工作。下面是一个使用它并将参数传递回的示例

function getProgress(hash){
   $.ajax({
        type: 'GET',
        url: 'http://domain.com/script.php?hash=' + hash,
        cache: false,
        dataType: 'json',
        success: function(data) {
          if (data < 100) {
            setTimeout(function() {
              getProgress(hash);
            }, 1000);
          } else {
            $('#progress-bar').hide();
          }
        },
    });
}
函数getProgress(散列){ $.ajax({ 键入:“GET”, 网址:'http://domain.com/script.php?hash=“+hash, cache:false, 数据类型:“json”, 成功:功能(数据){ 如果(数据<100){ setTimeout(函数(){ 获取进度(哈希); }, 1000); }否则{ $(“#进度条”).hide(); } }, }); }
请查看已接受的答案:请发布您如何使用
var interval
clearInterval
既然您正在使用
setInterval
您的函数将被调用,无需使用
getProgress()
手动调用它,为什么在初始调用
getProgress
时将
hash
作为参数传递,但不在
success
回调中<通过对
getProgress
的n次调用,code>hash将在第二时间
undefined
?如果响应时间超过1秒,预期结果是什么?太好了!非常感谢你的解释。我用以前的代码尝试了
setTimeout
,但无法使其工作,因此我将其更改为
setInterval
。哎呀!:)
function getProgress(hash){
   $.ajax({
        type: 'GET',
        url: 'http://domain.com/script.php?hash=' + hash,
        cache: false,
        dataType: 'json',
        success: function(data) {
          if (data < 100) {
            setTimeout(function() {
              getProgress(hash);
            }, 1000);
          } else {
            $('#progress-bar').hide();
          }
        },
    });
}