简体   繁体   English

我无法停止setInterval()

[英]I can't stop setInterval()

    var radiobuttons = $('input[name="slider"]');
    radiobuttons.eq(0).prop( "checked", true );
    var defalutvar = 0;

   function ani(a) {             
    function animate() {        
    var currentbutton = radiobuttons.eq(a);
        currentbutton.prop( "checked", true );
        a = (a + 1) % radiobuttons.length; //0, then 1, then 2, then 0, then 1, then 2, then 0, ...
    }        
    var rollDemRadios = setInterval(animate,1000);
    }

    $( document ).ready(ani(defalutvar)); 


    $('#active label').click(function () {            
        clearInterval(rollDemRadios); 
    });

This is my code. 这是我的代码。 I created a function ani to set a starting variable for a loop. 我创建了一个函数ani来设置循环的起始变量。 Now i can't stop setInterval. 现在我无法停止setInterval。 Someone can help me? 有人可以帮我吗?

Update: 更新:

    var radiobuttons = $('input[name="slider"]');
    radiobuttons.eq(0).prop( "checked", true );
    var defalutvar = 0;
    var negative = -1;

   function ani(a) {             
    function animate() {        
    var currentbutton = radiobuttons.eq(a);
        currentbutton.prop( "checked", true );
        a = (a + 1) % radiobuttons.length; //0, then 1, then 2, then 0, then 1, then 2, then 0, ...
    }
         var rollDemRadios = setInterval(animate,1000);
         $('#active label').click(function () {            
            clearInterval(rollDemRadios);  
        });
    }

    ani(0); 


    $('#active label').click(function () {
        i = $('input[name="slider"]:checked').index();
        ani(i);            
   });

Sorry. 抱歉。 I will say clearly my idea. 我会说清楚我的想法。 I am coding an java-based autoplay for a css-only slider. 我正在为仅css滑块编码基于Java的自动播放。 It will autoplay first then when I click a node it would stop and refresh with a new starting variable. 它会先自动播放,然后当我单击一个节点时它将停止并使用新的起始变量刷新。 My code is not working correctly, it continuouses at old position that it was running. 我的代码无法正常工作,它在运行时的旧位置继续运行。 It is my real problem. 这是我真正的问题。 Sorry all. 不好意思

First off, you're doing it in Javascript, not Java . 首先,您使用Javascript而不是Java进行操作

Second, this is locally scoped: 其次,这是本地范围的:

var rollDemRadios = setInterval(animate,1000);

meaning it exists only within the context of the animate function. 表示它仅存在于animate功能的上下文中。

To fix it, declare it globally and then just assign it a value inside the animate function. 要修复此问题,请全局声明它,然后在animate函数中为其分配一个值。 So, at the top of the code where you're declaring other variables, put this: 因此,在声明其他变量的代码顶部,输入以下内容:

var rollDemRadios;

and then instead of: 然后代替:

var rollDemRadios = setInterval(animate,1000);

put just: 放在:

rollDemRadios = setInterval(animate,1000);

POST-UPDATE 更新后

The main problem is that you have two click event listener on #active label and when you click, it does clear the current interval, but it also creates a new one because it calls ani(i); 主要问题是您在#active label上有两个click事件侦听器,并且单击时确实清除了当前间隔,但是由于调用了ani(i); ,因此它还会创建一个新的间隔ani(i); . Not only that, but by calling ani(i); 不仅如此,还可以通过调用ani(i); you're also creating a new set of listeners, which keep piling up and might cause your page to hang after a while. 您还将创建一组新的侦听器,这些侦听器会不断堆积,并可能导致页面在一段时间后挂起。 I'd suggest refactoring the ani function. 我建议重构ani函数。 Something like this: 像这样:

 var radiobuttons = $('input[name="slider"]'); radiobuttons.eq(0).prop("checked", true); var defalutvar = 0; var negative = -1; var rollDemRadios; function ani(a) { function animate() { var currentbutton = radiobuttons.eq(a); currentbutton.prop("checked", true); a = (a + 1) % radiobuttons.length; //0, then 1, then 2, then 0, then 1, then 2, then 0, ... } rollDemRadios = setInterval(animate, 1000); } ani(0); $('#active label').click(function() { if (rollDemRadios) { clearInterval(rollDemRadios); rollDemRadios = false; $('#on').removeClass('active'); $('#off').addClass('active'); } else { i = $('input[name="slider"]:checked').index(); ani(i); $('#off').removeClass('active'); $('#on').addClass('active'); } }); // irrelevant styling $('#on').addClass('active'); 
 #on.active {color: green} #off.active {color: red} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <input type="radio" name="slider"> <input type="radio" name="slider"> <input type="radio" name="slider"> </div> <div id="active"> <label>Toggle <span id="on">On</span>/<span id="off">Off</span> </label> </div> 

variable rollDemRadios to be declared as global scoped variable as shown below, otherwise variable rollDemRadios is considered as undefined within clearInterval(rollDemRadios); 变量rollDemRadios声明为全局范围的变量,如下所示,否则,在clearInterval(rollDemRadios);中,变量rollDemRadios被视为undefined clearInterval(rollDemRadios);

    var radiobuttons = $('input[name="slider"]');
    radiobuttons.eq(0).prop("checked", true);
    var defalutvar = 0;
    var rollDemRadios = null;

    function ani(a) {
        function animate() {
            var currentbutton = radiobuttons.eq(a);
            currentbutton.prop("checked", true);
            a = (a + 1) % radiobuttons.length; //0, then 1, then 2, then 0, then 1, then 2, then 0, ...
        }
        rollDemRadios = setInterval(animate, 1000);
    }

    $(document).ready(ani(defalutvar));


    $('#active label').click(function () {
        clearInterval(rollDemRadios);
    });
var radiobuttons = $('input[name="slider"]');
    radiobuttons.eq(0).prop( "checked", true );
    //var defalutvar = 0; //is never used
    //var negative = -1; //is never used

   var rollDemRadios;
   function ani(a) {                 
      rollDemRadios = setInterval(function() {
        var currentbutton = radiobuttons.eq(a);
        currentbutton.prop( "checked", true );
        a = (a + 1) % radiobuttons.length;
      },1000);
    }

    $('#active label').click(function () {            
       clearInterval(rollDemRadios);  
    });
    ani(0); 


    $('#active label').click(function () {
        i = $('input[name="slider"]:checked').index();
        ani(i);            
   });

The doc to help you how to use setInterval . 该文档可帮助您如何使用setInterval

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

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