简体   繁体   English

jQuery / JS:OnFocus OnBlur?

[英]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? 嘿,如果它失去了我网站的焦点1分钟,我想打一个ajax调用,所以当它再次处于焦点状态时,它会发送一个ajax调用? How can i do that? 我怎样才能做到这一点?

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;
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM