Skip to content Skip to sidebar Skip to footer

How To Pause And Resume Jquery Interval

I have made a custom slider with jQuery. For this I have used setInterval function: timer = setInterval(function() {}, 8000); But I cannot pause and resume the interval. I have 2

Solution 1:

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>); //createwindow.clearInterval(id); //pause
id = window.setInterval(<code>); //resume

Solution 2:

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;

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

Solution 3:

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;

functionSlide(){

    timer = setTimeout(function(){

        if(doSlide == true){
            Slide();
            i++; // Same as i = i + 1console.log('Sliding');

            if(i == 3) AbortSlide(); /* Abort on third slide! Dont use this in your logic!*/

        } elseif(doSlide == false){
            console.log('Sliding aborted untill next RunSlide() call!')
            clearTimeout(timer);
        }

    },1000);

}

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

functionRunSlide(){
    doSlide = true;
    Slide();
}

RunSlide();

You could also empty the interval in the abort method:

functionAbortSlide(){
    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!

Post a Comment for "How To Pause And Resume Jquery Interval"