Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery函数没有停止_Jquery_Loops_Intervals - Fatal编程技术网

jquery函数没有停止

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

我试图通过单击div停止我的函数,但函数没有停止!我犯了什么错误? 我试图改变重复我的函数的间隔,但它不起作用(( 这是我的密码

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语句中?