Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何停止/清除间隔_Javascript_Jquery - Fatal编程技术网

Javascript 如何停止/清除间隔

Javascript 如何停止/清除间隔,javascript,jquery,Javascript,Jquery,我正试图停止/清除间隔,但出现了一个错误 代码如下: function play(){ function sequencePlayMode() { var arg1;//some arguments var arg2;//some arguments changeBG(arg1,arg2);//here is function that changes the background } var tim

我正试图停止/清除间隔,但出现了一个错误

代码如下:

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

有人能帮我吗?

您需要使用存储函数的变量,而不是调用它的函数。您还需要使其他函数可以访问该变量

(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副本-当然……我真傻……我怎么会错过这个!非常感谢