
Question:
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 buttons (play, pause) which I want to use for. Lets say I click pause after 3 sec, and then resume it. So it should stay in that slider for 5 more seconds and then go to the next one and continue 8 seconds each. I have seen this kinda slider with mouseover pause, but can't do it by myself. I have tried this:
clearInterval(timer);
But this seems reset the interval, don't pause. Can anyone help :)
Answer1: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, <a href="https://code.google.com/p/jquery-timer/" rel="nofollow">the jquery-timer</a>
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(); //create
window.clearInterval(id); //pause
id = window.setInterval(); //resume
Answer2:
there are two ways of accomplish this:
<ol><li>Clearing the interval everytime you pause and starting a new interval when you resume it.
</li>
<li>Having a flag to tell the function in the interval when it is paused and it should not do anything.
</li>
</ol>The <strong>first solution</strong> 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 <strong>second solution</strong> 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;
};
Answer3:
The code looks fine.
But ... did you check if the <em>timer</em> variable is correctly set when you call the <em>clearInterval</em> function?
Answer4:
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: <a href="http://jchavannes.com/jquery-timer" rel="nofollow">http://jchavannes.com/jquery-timer</a>. 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: <a href="https://jsfiddle.net/q5qzmv68/7/" rel="nofollow">https://jsfiddle.net/q5qzmv68/7/</a>
Hope this helps! Cheers!