使用javascript的clearinterval
我制作了一个有间隔的动画。这是我的剧本:使用javascript的clearinterval,javascript,jquery,setinterval,intervals,Javascript,Jquery,Setinterval,Intervals,我制作了一个有间隔的动画。这是我的剧本: var count = 0; var countSecond = -40; function arrowAnimation() { if (count > 40) { clearInterval(); } $('.list-what-we-do .arrow').css({ top: (count++) + 'px' }); } function arrowAnimationSecon
var count = 0;
var countSecond = -40;
function arrowAnimation() {
if (count > 40) {
clearInterval();
}
$('.list-what-we-do .arrow').css({
top: (count++) + 'px'
});
}
function arrowAnimationSecond() {
if (countSecond > 0) {
clearInterval();
}
$('.list-what-we-do .arrow').css({
right: (countSecond++) + 'px'
});
}
setInterval(arrowAnimation, 5);
setInterval(arrowAnimationSecond, 5);
现在我的问题。我怎样才能停止间歇。我用了clearInterval。但这是行不通的。我怎样才能解决这个问题?谢谢你的帮助 使用
setInterval
或setTimeout
时,返回值为参考值;将此引用传递到clearInterval
以取消
var foo = setTimeout(...);
clearTimeout(foo);
使用
setInterval
或setTimeout
时,返回值为参考值;将此引用传递到clearInterval
以取消
var foo = setTimeout(...);
clearTimeout(foo);
必须将
setInterval
方法的返回值分配给变量
然后,您可以稍后使用clearInterval
var count = 0;
var countSecond = -40;
var interval = setInterval(arrowAnimation, 5);
var secondInterval = setInterval(arrowAnimationSecond, 5);
function arrowAnimation() {
if (count > 40) {
clearInterval(interval);
}
$('.list-what-we-do .arrow').css({
top: (count++) + 'px'
});
}
function arrowAnimationSecond() {
if (countSecond > 0) {
clearInterval(secondInterval);
}
$('.list-what-we-do .arrow').css({
right: (countSecond++) + 'px'
});
}
必须将
setInterval
方法的返回值分配给变量
然后,您可以稍后使用clearInterval
var count = 0;
var countSecond = -40;
var interval = setInterval(arrowAnimation, 5);
var secondInterval = setInterval(arrowAnimationSecond, 5);
function arrowAnimation() {
if (count > 40) {
clearInterval(interval);
}
$('.list-what-we-do .arrow').css({
top: (count++) + 'px'
});
}
function arrowAnimationSecond() {
if (countSecond > 0) {
clearInterval(secondInterval);
}
$('.list-what-we-do .arrow').css({
right: (countSecond++) + 'px'
});
}
您没有捕获设置间隔的返回值并将其用作清除间隔的参数。您没有捕获设置间隔的返回值并将其用作清除间隔的参数。