Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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
使用javascript的clearinterval_Javascript_Jquery_Setinterval_Intervals - Fatal编程技术网

使用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'
    });
}

您没有捕获设置间隔的返回值并将其用作清除间隔的参数。您没有捕获设置间隔的返回值并将其用作清除间隔的参数。