[英]clearInterval not working
First I have two divs box1
and box2
and i have these checkbox(s) which eventually it does stuff. 首先,我有两个div box1
和box2
,我有这些复选框,最终它可以完成工作。 One if these "stuff". 如果这些“东西”之一。 is blinking. 在闪烁。 so I made a function called blink
where it makes the div sort of blinking. 所以我做了一个叫做blink
的函数,它使div眨眼了。
I passed it through setInterval
so it would blink until I tell it otherwise. 我通过setInterval
传递了它,使其闪烁,直到我告诉它为止。 (I tell it to stop blinking with checkbox input) (我告诉它停止使用复选框输入闪烁)
now to break the Interval I used clearInterval
. 现在要打破间隔,我使用clearInterval
。 but when I do that I won't stop. 但是当我这样做时,我不会停止。 it does nothing or I am getting it wrong. 它什么都不做,或者我弄错了。
The idea here. 这里的想法。 when the page start, #box1
will be already blinking. 当页面开始时, #box1
将已经闪烁。 When checkbox #stopBlink
is checked. 选中#stopBlink
复选框后。 box2
should start blinking and box1
should stop. box2
应该开始闪烁,而box1
应该停止。 when #stopBlink
is unchecked box1
should start blinking and box2 should stops. 如果未选中#stopBlink
则box1
应该开始闪烁,而box2应该停止。
Script: 脚本:
function blink(text) {
$(text).fadeTo(400, 0.3).fadeTo(900, 1.0);
}
$(document).ready(function(){
box1_id = setInterval(function(){blink("#box1")}, 0);
$('input[type="checkbox"]').click(function() {
if($("#stopBlink").is(':checked'))
{
clearInterval(box1_id);
box2_id = setInterval(function(){blink("#box2")}, 0);
}else{
clearInterval(box2_id);
box1_id = setInterval(function(){blink("#box1")}, 0);
}
});
});
HTML: HTML:
<input type="checkbox" id="DoSomethingElse"> Do Something Else
<input type="checkbox" id="stopBlink"> Stop the Blinking
<div id="box1">BLINKING</div>
<div id="box2">NOT BLINKING</div>
change setInterval
time to match with total fade
time 更改setInterval
时间以匹配总fade
时间
which is 400 + 900 = 1300
那是400 + 900 = 1300
OR 要么
you can do same with CSS 你可以用CSS做同样的事情
$(document).ready(function() { $("#stopBlink").change(function() { $("#box1, #box2").toggleClass('blink'); }); });
.blink { animation: blink 600ms infinite alternate; } @keyframes blink { from { opacity: 1; } to { opacity: 0; } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" id="DoSomethingElse">Do Something Else <input type="checkbox" id="stopBlink">Stop the Blinking <div id="box1" class="blink">BLINKING</div> <div id="box2">NOT BLINKING</div>
The below addresses the code/logic aspects of the question - for your actual problem of blinking elements, you're better off using JAG's answer and use the CSS that modern browsers allow. 以下内容解决了该问题的代码/逻辑方面-对于闪烁元素的实际问题,最好使用JAG的答案,并使用现代浏览器允许的CSS。
There are two issues in the code you've posted. 您发布的代码中有两个问题。 Firstly, you're initially trying to clear a non-existent interval: 首先,您最初尝试清除一个不存在的时间间隔:
clearInterval(box_id);
resulting in this error: 导致此错误:
Uncaught ReferenceError: box_id is not defined 未捕获的ReferenceError:未定义box_id
If you fix that, and use: 如果您修复此问题,并使用:
clearInterval(box1_id)
It does correctly clear the interval and start the flashing of the 2nd div. 它会正确清除间隔并开始第二格的闪烁。 However, we then see the second issue. 但是,我们然后看到第二个问题。
Your intervals are all configured to use a 0ms timeout, so as soon as the page loads, you're generating hundreds of calls to blink
, as fast as the browser can call it. 您的时间间隔均配置为使用0ms超时,因此,在页面加载后,您就会生成数百个blink
的呼叫,其blink
速度与浏览器可以调用的速度一样。 This means when you click the checkbox, while the interval is cleared, jQuery has already queued up lots of animations, and the first div continues to blink while the second one then starts. 这意味着,当您清除间隔后,单击复选框时,jQuery已经将很多动画排队,并且第一个div继续闪烁,而第二个div则开始闪烁。
The quickest solution is to just set the intervals to have the same timeout as the animation times: 最快的解决方案是将间隔设置为与动画时间相同:
function blink(text) { $(text).fadeTo(400, 0.3).fadeTo(900, 1.0); } $(document).ready(function(){ box1_id = setInterval(function(){blink("#box1")}, 1300); $('input[type="checkbox"]').click(function() { if($("#stopBlink").is(':checked')) { clearInterval(box1_id); box2_id = setInterval(function(){blink("#box2")}, 1300); }else{ clearInterval(box2_id); box1_id = setInterval(function(){blink("#box1")}, 1300); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" id="DoSomethingElse"> Do Something Else <input type="checkbox" id="stopBlink"> Stop the Blinking <div id="box1">BLINKING</div> <div id="box2">NOT BLINKING</div>
This does result in an initial delay before they start blinking though, so if you want to keep it that they start blinking immediately, you might be better off refactoring slightly to have the blink function call itself: 但是,这的确会导致它们开始闪烁之前的初始延迟,因此,如果要使其立即开始闪烁,则最好稍微重构一下以让眨眼函数本身调用:
var timeout; function blink(text) { $(text).fadeTo(400, 0.3).fadeTo(900, 1.0); timeout = setTimeout(function() { blink(text); }, 1300); } $(document).ready(function() { blink("#box1"); $('input[type="checkbox"]').click(function() { clearTimeout(timeout); if ($("#stopBlink").is(':checked')) { blink("#box2") } else { blink("#box1") } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" id="DoSomethingElse">Do Something Else <input type="checkbox" id="stopBlink">Stop the Blinking <div id="box1">BLINKING</div> <div id="box2">NOT BLINKING</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.