Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding pause on hover to setInterval()?

Hi here's my jquery function for slideshow with other inbuilt function. I have been trying to make it pause when i hover over the overall div. Help me out!

$(function() {
var timer = setInterval( showDiv, 5000);
var counter = 2;
function showDiv() {

if (counter ==0) { counter++; return; }

$('div','#slide-show-overall')
  .stop()
  .fadeOut('slow')
  .filter( function() { return this.id.match('picture-set-' + counter); })   
  .fadeIn('slow');
  if (counter ==2) { slideWings();} //extra functions
  if (counter ==1) { resetWings();} //extra functions
counter == 3? counter = 1 : counter++; 
}
});
});



<div style="position:absolute;width:1000px;height:600;top:0px;z-index:0;overflow:hidden;" id="slide-show-overall" >
<div id="picture-set-1" style="">
<img src="images/3.jpg" align=left width=1000>
</div>
<div id="picture-set-2" style="display:none;">
<img src="images/1.jpg" align=left width=1000>
<img src="images/wing_left.png" align=left height=200 width=200 style="margin-top:-900px;margin-left:230px;" id="wing-left-1">
<img src="images/wing_right.png" align=left height=200 width=200 style="margin-    top:-900px;margin-left:830px;" id="wing-right-1">
</div>
<div id="picture-set-3" style="display:none;">
<img src="images/5.jpg" align=left width=1000>
</div>
</div>
like image 620
Abhishek Umrao Avatar asked Jun 06 '12 11:06

Abhishek Umrao


People also ask

How do you pause a setInterval function?

To pause setInterval() functions with JavaScript, we call the clearInterval function. let doThis = null; const y = () => { //... }; doThis = setInterval(y, 1000); const yStart = () => { doThis = setInterval(y, 1000); }; const yStop = () => { clearInterval(doThis); };

Can you clearInterval within setInterval?

Calling clearInterval() inside setInterval() has no effect But after calling clearInterval(), it will continue to execute.

Which function will stop the continuation of setInterval function?

The JavaScript setInterval() method returns an ID which can be used by the clearInterval() method to stop the interval. If you only need to execute a function one time, use the setTimeout() method.


1 Answers

You want to use clearInterval to remove the interval on hover and then replace it in the off hover function:

$('#slide-show-overall').hover(function(ev){
    clearInterval(timer);
}, function(ev){
    timer = setInterval( showDiv, 5000);
});
like image 164
mVChr Avatar answered Oct 22 '22 15:10

mVChr