2014-02-06 5 views
-1

간격을 지우거나 지우려고하지만 오류가 발생합니다. 아래중지/지우기 방법 간격

코드 :

function play(){ 
     function sequencePlayMode() { 
     var arg1;//some arguments 
     var arg2;//some arguments 
     changeBG(arg1,arg2);//here is function that changes the background 
     } 

    var time = 5000; 
    var timer = setInterval(sequencePlayMode, time);//this starts the interval and animation 

    } 


    function stop(){ 
     var stopPlay = clearInterval(sequencePlayMode);//here i'm getting error "sequencePlayMode is not defined" 
    } 


    $("#startAnimation").click(function(){ 
      play(); 
    }); 

    $("#stopAnimation").click(function(){ 
      stop(); 
    }); 

사람이 나를 좀 도와 드릴까요?

+0

clearInterval (타이머); 이 – laaposto

+0

가능한 복제본 [자바 스크립트에서 setInterval 호출 중지] (0120-139904)를 사용하십시오. – plalx

답변

5

함수를 저장하는 변수가 아니라 호출하는 함수가 있어야합니다. 또한 변수를 다른 함수에 액세스 가능하게해야합니다.

(function() { 

    var timer; //define timer outside of function so play and stop can both use it 
    function play(){ 
     function sequencePlayMode() { 
     var arg1;//some arguments 
     var arg2;//some arguments 
     changeBG(arg1,arg2);//here is function that changes the background 
     } 

    var time = 5000; 
    timer = setInterval(sequencePlayMode, time);//this starts the interval and animation 

    } 


    function stop(){ 
     var stopPlay = clearInterval(timer); 
    } 


    $("#startAnimation").click(function(){ 
      play(); 
    }); 

    $("#stopAnimation").click(function(){ 
      stop(); 
    }); 

})(); 
+0

+1. .silly me..how 나는 그것을 놓쳤다! 정말 고마워! :)) – Candide

+0

@epascarello 물론 적절한 폐쇄를 사용하는 경우 +1 – medzi