简体   繁体   English

如何停止/清除间隔

[英]how to stop/clear Interval

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? 有人可以帮我吗?

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

})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM