I’m doing a content slider that automatically cycles slides (by periodically calling the “next” function using setInterval)but stops when the user click on a prev/next buttons (by using clearInterval on the prev/next buttons). Is there a way to use setInterval again after a few seconds of clicking the button?
Code:
// start to automatically cycle slides
var cycleTimer = setInterval(function () {
$scroll.trigger('next');
}, 450);
// set next/previous buttons as clearInterval triggers
var $stopTriggers = $('img.right').add('img.left'); // left right
// function to stop auto-cycle
function stopCycle() {
clearInterval(cycleTimer);
}
Put your
setIntervalin a function and then call that function withsetTimeout().The difference between
setInterval()andsetTimeout()is thatsetInterval()calls your function repeatedly at each interval, whilesetTimeout()calls your function only once after the specified delay.In the code below I’ve added a function
startCycle(). Call that function to, well, start the cycle, both immediately so that it starts automatically and from a timeout set within your existingstopCycle()function.