stop settimeout in recursive function

后端 未结 7 2082
旧时难觅i
旧时难觅i 2020-12-01 11:24

my problem is that I can not stop a timer.

I had this method to set a timeout from this forum. It supposed to store the identifyer in the global variable. By accid

相关标签:
7条回答
  • 2020-12-01 11:43

    As noted above, the main reason why this code isn't working is that you're passingt he wrong thing into the clearTimeout call - you need to store the return value of the setTimeout call you make in updateFunction and pass this into clearTimeout, instead of the function reference itself.

    As a second suggestion for improvement - whenever you have what you call a recursive timeout function, you would be better off using the setInterval method, which runs a function at regular intervals until cancelled. This will achieve the same thing you're trying to do with your updateFunction method, but it's cleaner as you only need to include the "do stuff" logic in the deferred function, and it's probably more performant as you won't be creating nested closures. Plus it's The Right way to do it which has got to count for something, right? :-)

    0 讨论(0)
  • 2020-12-01 11:49

    (function(){

    $('#my_div').css('background-color', 'red');
    $('#my_div').hover(function(){
    var id=setTimeout(function() {
        $('#my_div').css('background-color', 'green');
    }, 2000);
    
    var id=setTimeout(function() {
        $('#my_div').css('background-color', 'blue');
    }, 4000);
    var id=setTimeout(function() {
        $('#my_div').css('background-color', 'pink');
    }, 6000);
    
        })
    
    $("#my_div").click(function(){  
            clearTimeout(id);
    
            })
    

    })();

    0 讨论(0)
  • 2020-12-01 11:52

    This is a simple pseudocode for controlling and conditioning recursive setTimeout functions.

        const myVar = setTimeout(function myIdentifier() {
    
        // some code
    
        if (condition) {
            clearTimeout(myIdentifier)
        } else {
            setTimeout(myIdentifier, delay); //delay is a value in ms.
        }
    
    }, delay);
    
    0 讨论(0)
  • 2020-12-01 11:55

    I think you misunderstand 'setTimeout' and 'clearTimeout'.

    If you want to set a timer that you want to cancel later, do something like:

    foo = setTimeout(function, time);
    

    then call

    clearTimeout(foo);
    

    if you want to cancel that timer.

    Hope this helps!

    0 讨论(0)
  • 2020-12-01 11:59

    I think that most people are getting at the reason why this isn't working, but I thought I would provide you with updated code. It is pretty much the same as yours, except that it assigns the timeout to a variable so that it can be cleared.

    Also, the anonymous function in a setTimeout is great, if you want to run logic inline, change the value of 'this' inside the function, or pass parameters into a function. If you just want to call a function, it is sufficient to pass the name of the function as the first parameter.

    var timer = null; 
    
    var updatetimer = function () {
        //do stuff
    
        // By the way, can just pass in the function name instead of an anonymous
        // function unless if you want to pass parameters or change the value of 'this'
        timer = setTimeout(updatetimer, 10000);
    };
    
    //this should start and stop the timer
    $("#mybutton").click(function(e) { 
         e.preventDefault();
         if($('#mydiv').is(':visible')){
            $('#mydiv').fadeOut('normal');
            clearTimeout(timer);  // Since the timeout is assigned to a variable, we can successfully clear it now
    
        } else{
            $('#mydiv').fadeIn('normal');
            updatetimer();
       }
    });
    
    0 讨论(0)
  • 2020-12-01 12:05

    You can not stop all the functions that are created, intead of that convert the function to setInterval (represent the same logic that your recursive function) and stop it:

    // recursive
    var timer= function () {
    // do stuff
      setTimeout(function (){timer();}, 10000);
    }
    

    The same logic using setInterval:

     // same logic executing stuff in 10 seconds loop
     var timer = setInterval(function(){// do stuff}, 10000)
    

    Stop it:

     clearInterval(timer);
    
    0 讨论(0)
提交回复
热议问题