亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

停止setInterval

停止setInterval

至尊寶的傳說 2019-08-13 14:08:13
停止setInterval我想在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         }     });}
查看完整描述

3 回答

?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

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


查看完整回答
反對 回復 2019-08-13
?
臨摹微笑

TA貢獻1982條經驗 獲得超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);
      }
    });
  }


查看完整回答
反對 回復 2019-08-13
?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

您必須將setInterval函數的返回值分配給變量

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

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


查看完整回答
反對 回復 2019-08-13
  • 3 回答
  • 0 關注
  • 761 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號