-1

I'm trying to stop/clear the interval but i'm getting an error.

code below:

   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();   
    });

Can someone help me with this please?

medzi
  • 407
  • 1
  • 8
  • 20

1 Answers1

5

You need to use the variable that you store the function in and not the function that you call it. You also need to make the variable accessible to the other function.

(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();   
    });

})();
epascarello
  • 204,599
  • 20
  • 195
  • 236