clearInterval对我不起作用

时间:2014-12-19 17:24:20

标签: javascript jquery

var waveTimes = 0;
var detectInterval = setInterval(function(){
    if(parseInt($(".people").css("top")) > 420){
        var waveInterval = setInterval(peopleWave,300);
        clearInterval(detectInterval);
    }
},300);
function peopleWave(){
    waveTimes += 1;
    if(waveTimes == 6){
        clearInterval(waveInterval);
    }
    var pic1 = "images/index/wave1.png";
    var pic2 = "images/index/wave2.png";
    if($(".wave img").attr("src") == pic1){
        $(".wave img").attr("src",pic2);
    } else {
        $(".wave img").attr("src",pic1);
    }
}

它表示在peopleWave运行6次后找不到waveInterval,我该如何解决呢?

1 个答案:

答案 0 :(得分:3)

在匿名函数之外定义waveInterval,以便peopleWave可以访问它:

var waveTimes = 0,
    waveInterval;
...
waveInterval = setTimeout(peopleWave, 300);