NineCattoRules
NineCattoRules

Reputation: 2428

Is it possible to stop a jQuery Timeout function when a user leaves the page?

I set a timer into a view using the jQuery:

var timer, myDiv = $('#mydiv');
$(document).on('mousemove', function(ev) {
    var _self = $(ev.target);

    clearTimeout(timer);

    if (_self.attr('id') === 'mydiv' || _self.parents('#mydiv').length) {
        return;
    }    

    if(!myDiv.hasClass('show')) {
       myDiv.fadeIn();
    }          

    timer = setTimeout(function() { 
        myDiv.fadeOut(1000, function() {
            myDiv.removeClass('show');
        });
    }, 1960);    
});

I need to use this timer in one only single page for all the time a user still there.

Is it possible to use jQuery or JavaScript to stop the timer when a user leaves the page and goes through another view?

here a DEMO

Upvotes: 1

Views: 652

Answers (1)

Eun
Eun

Reputation: 4178

use clearTimeout:

clearTimeout(mytimer);

Upvotes: 1

Related Questions