Karem
Karem

Reputation: 18123

jquery/js: OnFocus OnBlur?

Hey, i wish to make an ajax call if it has lost the focus from my site for 1 minute, so when it's onfocus again it send an ajax call? How can i do that?

Upvotes: 1

Views: 781

Answers (1)

user113716
user113716

Reputation: 322602

This seems to work. Give it a try:

$(function() {
    var tracktime;

    $(window).blur(function() {
        tracktime = new Date();
    })
    .focus(function() {
        if(tracktime && new Date() - tracktime > 60000) {
            $.ajax({
                url:'/your/path/',
                success:function() {
                    alert('ajax returned')
                }
            })
        }
        tracktime = null;
    });
});

Upvotes: 1

Related Questions