JQuery setTimeout

I would like to add a timeout to this hint code so that it only appears when the mouse hangs over it after a while, and not immediately ... I tried to add setTimeout() , but I could not figure out how to use clearTimeout() , so the tooltip is not hidden from the mouse. You can help?

 // ----------------------------------------------- // TOOLTIP MOUSE HOVER // ----------------------------------------------- function mcTooltip() { $('.mcTxb').mousemove(function(e) { var mcHoverText = $(this).attr('alt'); var mcTooltip = $('.mcTooltip'); $(mcTooltip).text(mcHoverText).show('fast'); $(mcTooltip).css('top', e.clientY + 10).css('left', e.clientX + 10); }).mouseout(function() { var mcTooltip = $('.mcTooltip'); $(mcTooltip).hide('fast'); }); } mcTooltip(); 

I tried this:

  // ----------------------------------------------- // TOOLTIP MOUSE HOVER // ----------------------------------------------- function mcTooltip() { $('.mcTxb').mousemove(function(e) { var mcHoverText = $(this).attr('alt'); var mcTooltip = $('.mcTooltip'); setTimeOut(function(){ $(mcTooltip).text(mcHoverText).show('fast'); }, 300); $(mcTooltip).css('top', e.clientY + 10).css('left', e.clientX + 10); }).mouseout(function() { var mcTooltip = $('.mcTooltip'); $(mcTooltip).hide('fast'); }); } mcTooltip(); 
+8
jquery settimeout
source share
6 answers

When you use animation, you can use .delay() to defer the appearance of your tooltip:

 $(mcTooltip).text(mcHoverText).delay(1000).show('fast'); 

In your mouseout function mouseout use .stop to undo any existing delays or animations, and then hide the tooltip:

 $(mcTooltip).stop(true).hide('fast'); 
+13
source share
 var my_timer; $('.mcTxb').hover( function () { my_timer = setTimeout(function () { //do work here }, 500); }, function () { clearTimeout(my_timer); } ); 

It will wait half a second before doing anything when you mouseover an element and all that won't happen if you mouseout for half a second.

+4
source share

One option is to use the hoverIntent plugin to accomplish what you want.

+2
source share
  • Use hover() instead, this is less code (and this is always good, IMO).

Same:

 function mcToolTip() { $(".mcTxb").hover(function(){ // mouseover stuff here $("element").delay(3000).show(); // 3 second delay, then show }, function(){ // mouseout stuff here }); } 
0
source share

This question is old, but I thought I would answer it to others. The main reason the timeout did not work is the case of "setTimeOut". You cannot ride a camel. Its "setTimeout".

0
source share

setTimeout does not work on mouseover or hover. It is safe to use .delay ().

 setTimeout(function(){ $(mcTooltip).text(mcHoverText).show('fast'); }, 300); // use this instead. $(mcTooltip).text(mcHoverText).delay(3000).show('fast'); 
0
source share

All Articles