How to make a setInterval stop after some time or after a number of actions?

前端 未结 4 931
醉梦人生
醉梦人生 2020-11-28 07:46

I\'ve created a loop of "changing words" with jQuery by using the code in this answer: jQuery: Find word and change every few seconds

How do I stop it after

相关标签:
4条回答
  • 2020-11-28 08:23

    Use clearInterval to clear the interval. You need to pass the interval id which you get from setInterval method.

    E.g.

    var intervalId = setInterval(function(){
                        ....
                     }, 1000);
    

    To clear the above interval use

    clearInterval(intervalId);
    

    You can change your code as below.

    (function(){
    
        // List your words here:
        var words = [
            'Lärare',
            'Rektor',
            'Studievägledare',
            'Lärare',
            'Skolsyster',
            'Lärare',
            'Skolpsykolog',
            'Administratör'
            ], i = 0;
    
        var intervalId = setInterval(function(){
            $('#dennaText').fadeOut(function(){
                $(this).html(words[i=(i+1)%words.length]).fadeIn();
                if(i == words.length){//All the words are displayed clear interval
                     clearInterval(intervalId);
                }
            });
           // 2 seconds
        }, 2000);
    
    })();
    
    0 讨论(0)
  • 2020-11-28 08:27

    You should consider using a recursive setTimeout() instead of setInterval() to avoid a race condition.

    var fadecount = 1;
    (function interval(){  
        $('#dennaText').fadeOut(function(){
            $(this).html(words[i=(i+1)%words.length]).fadeIn('fast',function(){
                if (fadecount < 30){
                    fadecount += 1;
                    setTimeout(interval, 2000);
                }
            });
        });
    }());
    
    0 讨论(0)
  • 2020-11-28 08:32

    To stop it after running a set number of times, just add a counter to the interval, then when it reached that number clear it.

    e.g.

    var timesRun = 0;
    var interval = setInterval(function(){
        timesRun += 1;
        if(timesRun === 60){
            clearInterval(interval);
        }
        //do whatever here..
    }, 2000); 
    

    If you want to stop it after a set time has passed (e.g. 1 minute) you can do:

    var startTime = new Date().getTime();
    var interval = setInterval(function(){
        if(new Date().getTime() - startTime > 60000){
            clearInterval(interval);
            return;
        }
        //do whatever here..
    }, 2000);     
    
    0 讨论(0)
  • 2020-11-28 08:40

    You can use setTimeout instead, which is better:

    (function foo(){ // wrap everything in a self-invoking function, not to expose "times"
      times = 20; // how many times to run
      (function run(){
        // do your stuff, like print the iteration
        document.body.innerHTML = times;
    
        if( --times ) // 200 * 20 = 4 seconds
          setTimeout(run, 100);
      })();
    })();

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