[英]Can anyone tell me why this is working once?
I created a simple rainbow in SCSS & jQuery and it works fine, only once. 我在SCSS和jQuery中创建了一个简单的Rainbow,它只能正常运行一次。 Can someone tell me why it doesn't execute multiple times?
有人可以告诉我为什么它不能多次执行吗?
JS JS
function Raibow() {
$('#rainbow').show();
setTimeout(function() {
$('#rainbow .rainbow').addClass('rainbow-effect');
}, 100);
$n = 5; $('#rainbow-timing').text($n);
setTimeout(function() {
$('#rainbow-message').show('fade', 400);
}, 900);
setTimeout(function() {
function count() {
$n--;
if ($n <= 0) {
$('#rainbow-message').hide('fade', 400);
$('#rainbow .rainbow').removeClass('rainbow-effect');
setTimeout(function() {
$('#rainbow-timing').text($n);
}, 200);
setTimeout(function() {
$('#rainbow').hide();
}, 1000);
clearInterval($n);
return;
}
$('#rainbow-timing').text($n);
}
setInterval(count, 1000);
}, 1400);
}
$('button').click(Raibow);
It's a jQuery problem? 这是jQuery问题吗? Or the animation works strange?
还是动画效果很奇怪?
You are not clearing the interval properly. 您没有正确清除间隔。
As per the Docs 根据文档
The returned timeoutID is a numeric, non-zero value which identifies the timer created by the call to setInterval(); 返回的timeoutID是一个非零的数字值,它标识通过调用setInterval()创建的计时器。 this value can be passed to Window.clearInterval() to cancel the timeout.
该值可以传递给Window.clearInterval()来取消超时。
https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval https://developer.mozilla.org/zh-CN/docs/Web/API/WindowTimers/setInterval
so you need to pass the timeoutID refreshIntervalId in the below example to clearinterval() 因此,您需要在以下示例中将timeoutID refreshIntervalId传递给clearinterval()
var refreshIntervalId;
function count() {
$n--;
if ($n <= 0) {
$('#rainbow-message').hide('fade', 400);
$('#rainbow .rainbow').removeClass('rainbow-effect');
setTimeout(function() {
$('#rainbow-timing').text($n);
}, 200);
setTimeout(function() {
$('#rainbow').hide();
}, 1000);
clearInterval(refreshIntervalId);
return;
}
$('#rainbow-timing').text($n);
}
setTimeout(function() {
refreshIntervalId = setInterval(count, 1000);
}, 1400);
Fiddle: https://jsfiddle.net/405zqzef/ 小提琴: https : //jsfiddle.net/405zqzef/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.