简体   繁体   English

如何停止此jQuery setInterval函数

[英]how to stop this jquery setInterval function

I have a timer function that, when it gets below 1 minute, calls this function to flash certain text. 我有一个计时器函数,当它低于1分钟时,调用此函数来刷新某些文本。 (based on this jsfiddle: http://jsfiddle.net/8yned9f9/5/ ) (基于此jsfiddle: http : //jsfiddle.net/8yned9f9/5/

$(document).ready(function(){
                setInterval(function(){
                    $('.flash').toggleClass('active');
                }, 500);
            });

After the minute expires, I need to STOP toggling the class and leave it with .active turned off. 分钟过后,我需要停止切换课程,并保持关闭.active的状态。 I'm unsure how to do this. 我不确定该怎么做。

Keep in mind that there is a js window.setInterval timer that must continue to run, only the jquery call should stop. 请记住,有一个js window.setInterval计时器必须继续运行,只有jquery调用应该停止。

UPDATE 更新

Based on some answers here I have added a variable so I can call clearInterval(flash) 根据此处的一些答案,我添加了一个变量,因此可以调用clearInterval(flash)

flash = setInterval(function(){
                            $('.flash').toggleClass('active');
                        }, 500);

That works as far as stopping the interval from firing. 只要停止发射间隔就可以了。 But it stops in the wrong state. 但是它停止在错误的状态。 Is there a way to then remove the .active class after stopping the interval? 有没有一种方法可以在停止间隔后删除.active类?

Update 2, solved removing the class with a quick search. 更新2,解决了通过快速搜索删除类的问题。

document.getElementById("seconds").classList.remove("active");

You need to assign your interval to a variable and use the variable to stop it with clearInterval 您需要将间隔分配给变量,然后使用该变量通过clearInterval停止它

var variable = setInterval(examplefunction, 10000);

clearInterval(variable);

Edit: 编辑:

To remove the class, you need to do this: 要删除该类,您需要执行以下操作:

$("yourelement").removeClass( ".active" );

The setInterval function returns an interval id to be used in the case you want to stop the interval function. setInterval函数返回一个间隔ID ,该ID将在您要停止间隔函数的情况下使用。 You can stop it by using the id as a parameter to the function clearInterval . 您可以通过使用id作为函数clearInterval的参数来停止它。

In your example: 在您的示例中:

$(document).ready(function(){
    var intervalId = setInterval(function(){
        $('.flash').toggleClass('active');
    }, 500);
    // stop after 60 seconds
    setTimeOut(function() {
        clearInterval(intervalId);
    }, 60000)
});

The setInterval function belongs to the window object and is not related to jquery. setInterval函数属于window对象,与jquery不相关。 You can find more information about setInterval here 您可以在此处找到有关setInterval更多信息。

setInterval() returns an id. setInterval()返回一个id。 save this id somewhere in a global variable of some sort. 将此ID保存在某种全局变量中。

Let's say you saved it to a variable "interval". 假设您将其保存到变量“ interval”。 You can then stop the interval by calling clearInterval(interval) . 然后,您可以通过调用clearInterval(interval)来停止clearInterval(interval)

Simplest would be to just add a condition in your script: 最简单的方法是在脚本中添加一个条件:

$(document).ready(function () {
  var count = 1;
  setInterval(function () {
    if (count < 120) {
      $('.flash').toggleClass('active');
      count++;
    }
  }, 500);
});

However, it is unclear to me why you would want the setInterval to continue if it does nothing. 但是,我不清楚,如果不执行任何操作,为什么要让setInterval继续。

var variable = setInterval(examplefunction, 10000); var variable = setInterval(examplefunction,10000);

clearInterval(variable); clearInterval(variable);

You could set another timer to deal with it after one minute, like this: 您可以设置一分钟后再处理另一个计时器,如下所示:

 $(document).ready(function(){ var id = setInterval(function(){ $('.flash').toggleClass('active'); }, 500); setTimeout(function () { clearInterval(id); // stop the setInterval $('.flash').addClass('active'); // make sure active is ON. }, 6000); // after 6 seconds (make this one minute if you want) }); 
 .flash {color: red} .flash.active {color: black} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="flash">Hello</div> 

For illustration purposes the flashing stops after 6 seconds. 出于说明目的,闪烁在6秒钟后停止。 Adapt as needed. 根据需要进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM