Javascript 停止设定间隔

Javascript 停止设定间隔,javascript,jquery,html,setinterval,Javascript,Jquery,Html,Setinterval,我想停止错误处理程序中的此间隔重复运行。这可能吗?如果可能,怎么可能 // example code $(document).on('ready',function(){ setInterval(updateDiv,3000); }); function updateDiv(){ $.ajax({ url: 'getContent.php', success: function(data){ $('.square').ht

我想停止
错误处理程序中的此间隔重复运行。这可能吗?如果可能,怎么可能

// 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
        }
    });
}
//示例代码
$(文档).on('ready',function(){
setInterval(updateDiv,3000);
});
函数updateDiv(){
$.ajax({
url:'getContent.php',
成功:功能(数据){
$('.square').html(数据);
},
错误:函数(){
$.playSound('oneday.wav');
$('.square').html('连接问题');
//我想在这里停止
}
});
}

您需要将
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>');
        }
    });
}
var区间=null;
$(文档).on('ready',function(){
间隔=设置间隔(更新DIV,3000);
});
函数updateDiv(){
$.ajax({
url:'getContent.php',
成功:功能(数据){
$('.square').html(数据);
},
错误:函数(){
clearInterval(interval);//停止该间隔
$.playSound('oneday.wav');
$('.square').html('连接问题');
}
});
}

使用变量并调用
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);
      }
    });
  }
var区间;
$(文档).on('ready',function()
间隔=设置间隔(更新DIV,3000);
});
函数updateDiv(){
$.ajax({
url:'getContent.php',
成功:功能(数据){
$('.square').html(数据);
},
错误:函数(){
$.playSound('oneday.wav');
$('.square').html('连接问题');
//我想在这里停止
间隔时间;
}
});
}

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

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

然后用它再次清除。

用这个我希望能帮助你

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");
var区间;
函数updateDiv(){
$.ajax({
url:'getContent.php',
成功:功能(数据){
$('.square').html(数据);
},
错误:函数(){
/*间隔时间*/
stopinterval();//停止间隔
$.playSound('oneday.wav');
$('.square').html('连接问题');
}
});
}
函数playinterval(){
updateDiv();
interval=setInterval(函数(){updateDiv();},3000);
返回false;
}
函数stopinterval(){
间隔时间;
返回false;
}
$(文件)
.on(“准备就绪”,播放间隔)
.on({click:playinterval},“#playinterval”)
.on({click:stopinterval},“#stopinterval”);
var闪光器图标=功能(obj){
var classtoggle=obj.classtoggle;
变量elem=对象targetElem;
var oneTime=目标速度;
var半闪光=一次/2;
var totalTime=obj.flashingTimes*oneTime;
var interval=setInterval(函数(){
元素addClass(ClassToToToggle);
setTimeout(函数(){
elem.removeClass(ClassToToToggle);
},半闪光);
},一次);
setTimeout(函数(){
间隔时间;
},总时间);
};
闪光器图标({
targetElem:$(“#icon-step-1-v1”),
闪现时间:3,
ClassToToToggle:“闪光器图标”,
速度:500
});
.steps图标{
背景#d8d8d8;
颜色:#000;
字体大小:55px;
填充:15px;
边界半径:50%;
保证金:5px;
光标:指针;
}
.闪光器图标{
颜色:#fff;
背景:#820000!重要;
填充底部:15px!重要;
填充顶部:15px!重要;
}


报警
我们可以通过调用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);

指向文档的链接:您还可以使用只运行一次的
setTimout()
。请解释此代码的作用以及它与问题的关系。可能重复非常糟糕的做法。第一,这可能意味着
setInterval
可能会发生不确定的泄漏-没有相关的var来控制它。第二,您无法清除
,因为
不是时间间隔。如果使用
TypeScript
您会遇到麻烦:
没有重载与此调用匹配。重载1/2'(intervalId:Timeout):void'出现以下错误:“this”类型的参数不能分配给“Timeout”类型的参数。