Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Using .one() with hover

Tags:

jquery

Is there any way to get a hover function to only execute once? This is what I'm currently trying:

$('#ask').live('hover', function() {

    $('#homesearch-after').hide(300);

    $.doTimeout( 300, function() {
        hideClosedSearchLink();
        showHomeSearch();
    });

});

But that's not working. How can I only get this hover to activate once?

I've tried changing .live with .one and .bind... resulting in nothingness.

like image 775
Jared Avatar asked Jan 09 '11 10:01

Jared


1 Answers

You've already answered yourself, use .one() (not .live()).

But as lasseespeholt just pointed out in a comment, .hover() is shorthand for binding to mouseenter and mouseleave, and is not an event in itself.

Try this:

$('#ask').one('mouseenter', function() {

    $('#homesearch-after').hide(300);

    $.doTimeout( 300, function() {
        hideClosedSearchLink();
        showHomeSearch();
    });

});

If that still doesn't work, try just using the good ol' .hover() and then .unbind()ing it immediately after it's finished.

$('#ask').hover(function() {

    $('#homesearch-after').hide(300);

    $.doTimeout( 300, function() {
        hideClosedSearchLink();
        showHomeSearch();
    });

    // Again taking into account what lasseespeholt said
    $(this).unbind('mouseenter mouseleave')

});
like image 160
BoltClock Avatar answered Oct 20 '22 02:10

BoltClock