停止 setInterval

IT技术 javascript jquery html setinterval
2021-01-16 08:23:43

我想阻止error处理程序中的这个间隔重复运行。这可能吗,如果可以,怎么做?

// example code
$(document).on('ready',function(){
    setInterval(updateDiv,3000);
});

function updateDiv(){
    $.ajax({
        url: 'getContent.php',
        success: function(data){
            $('.square').html(data);
        },
        error: function(){
            $.playSound('oneday.wav');
            $('.square').html('<span style="color:red">Connection problems</span>');
            // I want to stop it here
        }
    });
}
6个回答

您需要将 的返回值设置为setInterval点击处理程序范围内的变量,然后clearInterval()像这样使用

var interval = null;
$(document).on('ready',function(){
    interval = setInterval(updateDiv,3000);
});

function updateDiv(){
    $.ajax({
        url: 'getContent.php',
        success: function(data){
            $('.square').html(data);
        },
        error: function(){
            clearInterval(interval); // stop the interval
            $.playSound('oneday.wav');
            $('.square').html('<span style="color:red">Connection problems</span>');
        }
    });
}
您也可以使用setTimout()which 只运行一次
2021-03-19 08:23:43
2021-03-24 08:23:43

使用变量并调用clearInterval来停止它。

var interval;

$(document).on('ready',function()
  interval = setInterval(updateDiv,3000);
  });

  function updateDiv(){
    $.ajax({
      url: 'getContent.php',
      success: function(data){
        $('.square').html(data);
      },
      error: function(){
        $.playSound('oneday.wav');
        $('.square').html('<span style="color:red">Connection problems</span>');
        // I want to stop it here
        clearInterval(interval);
      }
    });
  }

您必须将setInterval函数的返回值分配给变量

var interval;
$(document).on('ready',function(){
    interval = setInterval(updateDiv,3000);
});

然后clearInterval(interval)再次使用清除它。

使用这个我希望对你有帮助

var interval;

function updateDiv(){
    $.ajax({
        url: 'getContent.php',
        success: function(data){
            $('.square').html(data);
        },
        error: function(){
            /* clearInterval(interval); */
            stopinterval(); // stop the interval
            $.playSound('oneday.wav');
            $('.square').html('<span style="color:red">Connection problems</span>');
        }
    });
}

function playinterval(){
  updateDiv(); 
  interval = setInterval(function(){updateDiv();},3000); 
  return false;
}

function stopinterval(){
  clearInterval(interval); 
  return false;
}

$(document)
.on('ready',playinterval)
.on({click:playinterval},"#playinterval")
.on({click:stopinterval},"#stopinterval");

我们可以通过调用 clear interval 轻松停止设置的间隔

var count = 0 , i = 5;
var vary = function intervalFunc() {
  count++;
      console.log(count);
    console.log('hello boy');  
    if (count == 10) {
      clearInterval(this);
    }
}

  setInterval(vary, 1500);
非常糟糕的做法。第一,这可能意味着它们setInterval可能会无限泄漏 - 没有关联的变量来控制它。第二,你不清楚this,因为this不是时间间隔。如果你使用TypeScript你会遇到麻烦:No overload matches this call.Overload 1 of 2, '(intervalId: Timeout): void', gave the following error: Argument of type 'this' is not assignable to parameter of type 'Timeout'.
2021-03-24 08:23:43