Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop timer on hover

I'm not a JS coder my any means. I know enough to make things do what I want, but couldn't code from scratch. My issue is:

We have a shopping cart that when you add a product the cart shows itself for 4 secs unless the customer hovers over the cart. I can't seem to get it to stop the timeout when the cursor is hovered over it.

$(document).ready(function () {
    setTimeout(function () { $('#ctl00_ctl00_ctlHeader_divOrderProducts').hide(); }, 4000);
});
like image 533
THEDert Avatar asked Jan 26 '26 12:01

THEDert


2 Answers

Store the return of setTimeout() in a variable, and use that to clearTimeout():

// t is a global scope variable.
// Probably a good idea to use something better than 't'
var t;
$(document).ready(function () {
  // Store the return of setTimeout()
  t = setTimeout(function () { $('#ctl00_ctl00_ctlHeader_divOrderProducts').hide(); }, 4000);
});

$('cart-selector').hover(function() {
   if (t) {
    // Call clearTimeout() on hover()
    clearTimeout(t);
   }
});
like image 134
Michael Berkowski Avatar answered Jan 29 '26 03:01

Michael Berkowski


You need to set your timer to a variable:

var timer1 = setTimeout(function () { ... })

then use:

clearTimeout(timer1)
like image 40
Diodeus - James MacFarlane Avatar answered Jan 29 '26 01:01

Diodeus - James MacFarlane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!