xSketchy0 xSketchy0 - 3 months ago 7
Javascript Question

Make JQuery event available after a certain amount of time

I'm trying to make one of those annoying popups when leaving your browser. However, I want the event to be available after a certain amount of time. The event should be allowed to trigger after a certain amount of time. I've seen stuff such as delay and setTimeout, but I have no idea how to implement it on my code.

JavaScript:

$(document).on("mouseleave", function (event) {
if (event.pageY < 0) {
$(".leavemodal").fadeIn(600);
}
});

Answer

This is not tested but maybe you can try this.

$(document).ready(function() {
    canRun = false;
    waitPeriod = 1000;// waiting time
    setTimeout(function() { canRun = true; }, waitPeriod);

    $(document).on("mouseleave", function (event) {
        if (!canRun) {
            return false;
        }
        if (event.pageY < 0) {
            $(".leavemodal").fadeIn(600);
        }
    });
});
Comments