jquery函数没有停止
我试图通过单击div停止我的函数,但函数没有停止!我犯了什么错误? 我试图改变重复我的函数的间隔,但它不起作用(( 这是我的密码jquery函数没有停止,jquery,loops,intervals,Jquery,Loops,Intervals,我试图通过单击div停止我的函数,但函数没有停止!我犯了什么错误? 我试图改变重复我的函数的间隔,但它不起作用(( 这是我的密码 Piano.anim = function(){ var monitorGlich = true; var interval = 100; var $glich = $('.monitor-glich'); if(monitorGlich){ setInterval(function(e) { s
Piano.anim = function(){
var monitorGlich = true;
var interval = 100;
var $glich = $('.monitor-glich');
if(monitorGlich){
setInterval(function(e) {
setTimeout(function () {
$glich.addClass('active');
},4000);
setTimeout(function () {
$glich.removeClass('active');
},1000);
}, interval);
}else{
$glich.removeClass('active');
}
$(".monitor").click(function(){
interval = 0;
monitorGlich = false;
console.log(interval);
});
};
在全局范围内保留一个变量(myIntVal=0;
),并在函数中作为
myIntVal = setInterval(function(e) {
//...
}, 4000);
在$(“.monitor”)。单击(…)
使用
$(".monitor").click(function(){
// ...
clearInterval(myIntVal); // clears the timer so loop stops
});
因此,
setInterval
将停止。您是否已将代码包装在document.ready语句中?