【问题标题】:Stop setInterval停止设置间隔
【发布时间】:2013-05-02 11:26:30
【问题描述】:

我想阻止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
        }
    });
}

【问题讨论】:

标签: javascript jquery html setinterval


【解决方案1】:

您需要将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>');
        }
    });
}

【讨论】:

【解决方案2】:

使用变量并调用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);
      }
    });
  }

【讨论】:

    【解决方案3】:

    你必须将setInterval函数的返回值赋给一个变量

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

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

    【讨论】:

      【解决方案4】:

      使用这个希望对你有帮助

      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");
      

      【讨论】:

        【解决方案5】:

        我们可以通过调用 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 可能会无限泄漏——没有关联的 var 来控制它。第二你不清楚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'.
        【解决方案6】:

        var flasher_icon = function (obj) {
            var classToToggle = obj.classToToggle;
            var elem = obj.targetElem;
            var oneTime = obj.speed;
            var halfFlash = oneTime / 2;
            var totalTime = obj.flashingTimes * oneTime;
        
            var interval = setInterval(function(){
                elem.addClass(classToToggle);
                setTimeout(function() {
                    elem.removeClass(classToToggle);
                }, halfFlash);
            }, oneTime);
        
            setTimeout(function() {
                clearInterval(interval);
            }, totalTime);
        };
        
        flasher_icon({
            targetElem: $('#icon-step-1-v1'),
            flashingTimes: 3,
            classToToggle: 'flasher_icon',
            speed: 500
        });
        .steps-icon{
            background: #d8d8d8;
            color: #000;
            font-size: 55px;
            padding: 15px;
            border-radius: 50%;
            margin: 5px;
            cursor: pointer;
        }
        .flasher_icon{
          color: #fff;
          background: #820000 !important;
          padding-bottom: 15px !important;
          padding-top: 15px !important;
        }
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 
        
        <i class="steps-icon material-icons active" id="icon-step-1-v1" title="" data-toggle="tooltip" data-placement="bottom" data-original-title="Origin Airport">alarm</i>

        【讨论】:

        • 请解释这段代码的作用以及它与问题的关系。
        猜你喜欢
        • 2016-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-19
        • 2019-09-08
        • 1970-01-01
        • 2019-02-16
        相关资源
        最近更新 更多