How to pause and resume jquery interval

前端 未结 3 887
粉色の甜心
粉色の甜心 2021-01-24 23:32

I have made a custom slider with jQuery. For this I have used setInterval function:

timer = setInterval(function() {},  8000);

But I cannot pau

相关标签:
3条回答
  • 2021-01-24 23:41

    I'm not entirely sure that's something native to jQuery, however, you could use a flag to pause it, and check in your setInterval whether to execute.

    Edit: Found something that might be useful to you, the jquery-timer

    Alternitively, you can keep track of the id set by setInterval, and clear out out when you'd like to pause. Then you can set it again when you wish to resume:

    var id = window.setInterval(<code>); //create
    window.clearInterval(id); //pause
    id = window.setInterval(<code>); //resume
    
    0 讨论(0)
  • 2021-01-24 23:59

    there are two ways of accomplish this:

    1. Clearing the interval everytime you pause and starting a new interval when you resume it.

    2. Having a flag to tell the function in the interval when it is paused and it should not do anything.

    The first solution would work like this:

    var intervalId = false;
    
    function intervalFunction () {
       // do stuff.
    }
    
    startButton.onclick = function () {
        if (intervalId === false) {
            intervalId = setInterval(intervalFunction, 8000);
        }
    }
    
    pauseButton.onclick = function () {
        if (intervalId !== false) {
            clearInterval(intervalFunction);
            intervalId = false;
        }
    }
    
    // auto start it:
    intervalId = setInterval(intervalId);
    

    The second solution would work like this:

    var isRunning = true;
    var interval = setInterval(function() {
        if (!isRunning) {
            // not running, do nothing
        } else {
            // it is running, do stuff.
        }
    }, 8000);
    
    pauseButton.onclick = function () {
        isRunning = false;
    };
    startButton.onclick = function () {
        isRunning = true;
    };
    
    0 讨论(0)
  • 2021-01-25 00:02

    I am not complete sure, that what you are asking for, is the right thing you are showing us... setInterval basically is pure native javascript and in my opinion not worth using! If you wan't to set your timeouts via jquery try this link: http://jchavannes.com/jquery-timer. You can find usages there...

    Now on to your problem... you need a state to check wether the slider has to slide or not... simply set a bool like this...

    var timer;
    var doSlide = false;
    var i = 0;
    
    function Slide(){
    
        timer = setTimeout(function(){
    
            if(doSlide == true){
                Slide();
                i++; // Same as i = i + 1
                console.log('Sliding');
    
                if(i == 3) AbortSlide(); /* Abort on third slide! Dont use this in your logic!*/
    
            } else if(doSlide == false){
                console.log('Sliding aborted untill next RunSlide() call!')
                clearTimeout(timer);
            }
    
        },1000);
    
    }
    
    function AbortSlide(){
        doSlide = false;
        i = 0;  // Resetting the count! Dont use this in your logic!
    }
    
    function RunSlide(){
        doSlide = true;
        Slide();
    }
    
    RunSlide();
    

    You could also empty the interval in the abort method:

    function AbortSlide(){
        doSlide = false;
        clearTimeout(timer);
        i = 0; // Resetting the count! Dont use this in your logic!
    }
    

    Here is a working fiddle i made for you to understand what timers and intervals are for: https://jsfiddle.net/q5qzmv68/7/

    Hope this helps! Cheers!

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