Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger jQuery mousemove event only once

I'm trying to make an exit popup and I could do that using the following code. Whenever the user's mouse move out of the browser area, this gives a popup. But it is quite annoying when the popup comes everytime. I want to limit it to just a single time. Can somebody help me with this?

jQuery(document).ready(function () {
    jQuery(document).mousemove(function(e) {
        jQuery('#exitpopup').css('left', (window.innerWidth/2 - jQuery('#exitpopup').width()/2));
        jQuery('#exitpopup').css('top', (window.innerHeight/2 - jQuery('#exitpopup').height()/2));
        if(e.pageY <= 5)
        {
            // Show the exit popup
            jQuery('#exitpopup_bg').fadeIn();
            jQuery('#exitpopup').fadeIn();
        }
    });
});
like image 359
Arjun S Kumar Avatar asked Feb 07 '26 14:02

Arjun S Kumar


1 Answers

Use jQuery's one() function: http://api.jquery.com/one/

jQuery(document).ready(function () {
        jQuery(document).one('mousemove', function(e) {
            jQuery('#exitpopup').css('left', (window.innerWidth/2 - jQuery('#exitpopup').width()/2));
            jQuery('#exitpopup').css('top', (window.innerHeight/2 - jQuery('#exitpopup').height()/2));
            if(e.pageY <= 5)
            {   
                // Show the exit popup
                jQuery('#exitpopup_bg').fadeIn();
                jQuery('#exitpopup').fadeIn();
            }
        });
});
like image 102
brokethebuildagain Avatar answered Feb 09 '26 12:02

brokethebuildagain



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!