05 March 2012

Introduction

I came across the need to detect and delay a hover before executing a function. The page was full of images, and when hovered over, would create an AJAX request to the server and display in a popup.

A Quick Example

Here is an example on how to achieve it.

$('img.expand').bind({ mouseover: function() { 
    obj = $(this); 
    jQueryTimeout = setTimeout(function() { 
        $.post('/target-uri', 
        { 'attribute1':obj.attr('title'), 'attribute2':obj.attr('href') }, 
        function(response){ 
            console.log(response); 
        }, 
        'json' ); 
        }, 
        2000); 
    }, 
    mouseleave: function() { 
        clearTimeout(jQueryTimeout); 
    } 
});

Demo

In the demo, there is a link, 'here', and if you hover over it for 2 seconds, it will display the title attribute in the input text field below.

This is a demo. Hover here:

Demo Code

<script>
$(document).ready(function(){
        $(document).ready(function(e) {

        mouseover: function() {
            obj = $(this);

            jQueryTimeout = setTimeout(function() {
                $('.demo-target').val(obj.attr('title'));
            }, 2000);
        },
        mouseleave: function() {
            clearTimeout(jQueryTimeout);
        }
    });
});
Tagged under hover, hover-timeout, javascript-mouseover, jquery-2, mouseout, mouseover, timeout, and others
Mike Mackintosh

This post was written by Mike Mackintosh, a decorated security professional.




Related Posts