0

間隔のあるアニメーションを作成しました。これは私のスクリプトです:

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 を使用しました。しかし、それは機能していません。どうすればこれを修正できますか? 助けてくれてありがとう!

4

2 に答える 2

5

setIntervalまたはsetTimeout戻り値が参照である場合。clearIntervalキャンセルするには、この参照を に渡します。

var foo = setTimeout(...);
clearTimeout(foo);
于 2012-12-21T12:38:54.673 に答える
0

メソッドの戻り値を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'
    });
}
于 2012-12-21T12:40:43.603 に答える