tags:

views:

78

answers:

2

I'm using this code in my site and I was wondering how I could add a delay to the mouseleave function

$target.mouseenter(function(e){
                var $tooltip=$("#"+this._tipid)
                ddimgtooltip.showbox($, $tooltip, e)
            })
            $target.mouseleave(function(e){
             var $tooltip=$("#"+this._tipid);
             setTimeout(function() { ddimgtooltip.hidebox($, $tooltip); }, 4000);
            })

            $target.mousemove(function(e){
                var $tooltip=$("#"+this._tipid)
                ddimgtooltip.positiontooltip($, $tooltip, e)
            })
            if ($tooltip){ //add mouseenter to this tooltip (only if event hasn't already been added)
                $tooltip.mouseenter(function(){
                    ddimgtooltip.hidebox($, $(this))
                })
+2  A: 

You can use setTimeout() and an anonymous function for this:

$target.mouseleave(function(e){
 var $tooltip=$("#"+this._tipid);
 setTimeout(function() { ddimgtooltip.hidebox($, $tooltip); }, 250);
})

This would delay it 250ms after leaving before it hides, you can just adjust that value as needed.

Nick Craver
thank you that's awesome, now when I mouseover another one is there a way I can skip that timeout?
Dustin McCarthy
@Dustin - Yep, but I can't say how exactly without seeing your `mouseenter` function for the other elements, need a bit more context added to the question.
Nick Craver
I updated the code with more for you, thanks for your help
Dustin McCarthy
@Dustin - What is `$target`? Since you'll have to target the rest of those elements to hide their tooltips.
Nick Craver
the whole script is too long to post is there an email or messenger I can send you the code at?
Dustin McCarthy
@Dustin - Check out jsfiddle or jsbin, for example: http://jsfiddle.net/nick_craver/bLQrn/4/
Nick Craver
A: 

The problem with just a timer would be if you mouse left and then re-entered it would still hide after that timer completed. Something like the following might work better because we can cancel the timer whenever the mouse enters the target.

var myTimer=false;
$target.hover(function(){
    //mouse enter
    clearTimeout(myTimer);
},
function(){
    //mouse leave
    var $tooltip=$("#"+this._tipid);
    myTimer = setTimeout(function(){
        ddimgtooltip.hidebox($, $tooltip);
    },500)
});
Chris Barr
I see what you mean, just tested the first function and it did exactly what you said, can you help edit my edited posts to make the change that you suggested?
Dustin McCarthy