Mike
Mike

Reputation: 3

Javascript setTimeout on Mouseleave event

I have a div that appears when the user's mouse leaves the document with a survey about my site.

I don't want to ask them to do the survey if they move their mouse out in the first couple of minutes while browsing around, so I was wondering if I can set a timeout/sleep on when this mouseleave function would activate.

Here is my code without the timeout:

        $(document).mouseleave(function () {
        document.getElementById("Overlay1").style.display = "";
        });

Thanks a lot!

Upvotes: 0

Views: 2047

Answers (2)

gion_13
gion_13

Reputation: 41533

you could call the same function encapsulated in asetTimeout function call :

 $(document).mouseleave(function() {
    var seconds = 3;

    //execute the function in 3 seconds
    setTimeout(function(){
        document.getElementById("Overlay1").style.display = "";
    },seconds*1000);
 });

Upvotes: 0

Niklas
Niklas

Reputation: 30002

You can use window.setTimeout to enable the function after a certain time.

Example:

    window.setTimeout(function(){
    $(document).mouseleave(function () {
            console.log('mouse leave');
            document.getElementById("Overlay1").style.display = "";
            });
        console.log('initiated');
    },5000);

Upvotes: 2

Related Questions