如何仅当用户存在(通过鼠标移动)时才使用 jQuery 执行 setTimeout()

发布于 2024-09-29 07:43:34 字数 383 浏览 7 评论 0原文

我有一个通过 AJAX 更新

的函数:

function update() {
    <!-- .ajax() --> 
    setTimeout(update(), 3000);}
}

我需要的是,当用户不在网站上时,不会执行此函数,因此如果没有移动鼠标(我们假设如果移动它是在网站中)它不会更新 .mousemove()。顺便问一下,我们还可以做其他事情来了解有人在网站上活跃吗?

这怎么能做到呢?先感谢您!

编辑:可能我解释得不好。我需要知道仅在有活动时更新的方法。就像 Facebook 对其新闻推送、首页所做的那样。谢谢!

I have a function that updates a <div /> via AJAX:

function update() {
    <!-- .ajax() --> 
    setTimeout(update(), 3000);}
}

What I need is that this is not executed when the user is not present on the website, so if there is no movement of the mouse (we will suppose that if move it is in the website) it will not update .mousemove(). By the way, there is any other thing that we can do to know is someone is active on the website?

How can this be done? Thank you in advance!

Edit: probably I explained bad. I need to know the way to only update when there is activity. Like Facebook does with his news feed, the front page. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼前雾蒙蒙 2024-10-06 07:43:34

您可以使用 mousemove 处理程序来跟踪用户上次移动的时间,然后仅当用户上次在 X 秒内移动鼠标时才会发生该过程。但是,当然,如果用户坐在那里阅读某些内容,或者他们是一个以键盘为导向的人,那么他们往往会错过他们在那里......所以你可能想看看 keydown 也是如此。

这是一个鼠标移动示例

jQuery(function($) {
    var count = 0, lastmove = new Date();

    $(document).mousemove(function() {
        ++count;
        lastmove = new Date();
        $('#display').html("Moved " + count + " times");
    });

});​

那么您的更新代码可以执行以下操作:

function update() {
    if (new Date() - lastmove < 60000) { // 60 seconds
        // Really do the update
    }
    else {
        // Check back in a few seconds
        setTimeout(update, 3000);
    }
}

偏离主题,但是您的更新代码有错误。您有:

setTimeout(update(), 3000);

...它将立即调用update,然后尝试使用其返回值来安排在三秒内发生的事情。如果您希望对 update 的调用安排在三秒内发生,请在其后面省略 ()

setTimeout(update, 3000);

You could use a mousemove handler to track when the user last moved, and then have the process only happen if they last moved the mouse within X seconds. But of course, if the user is sitting there reading something, or if they're a keyboard-oriented kind of person, that will tend to miss that they are there... So you'd probably want to look at keydown as well.

Here's a mousemove example:

jQuery(function($) {
    var count = 0, lastmove = new Date();

    $(document).mousemove(function() {
        ++count;
        lastmove = new Date();
        $('#display').html("Moved " + count + " times");
    });

});​

Then your update code could do this:

function update() {
    if (new Date() - lastmove < 60000) { // 60 seconds
        // Really do the update
    }
    else {
        // Check back in a few seconds
        setTimeout(update, 3000);
    }
}

Off-topic, but you have an error in your update code. You have:

setTimeout(update(), 3000);

...which will call update immediately and then try to use its return value to schedule something to happen in three seconds. If you want the call to update to be scheduled to happen in three seconds, leave off the () after it:

setTimeout(update, 3000);
情绪失控 2024-10-06 07:43:34

我想我可能会得到这样的结果。避免日期算术。只关心自上次 update() 以来是否有一些活动。

window.activeFlag = false;
window.updateDelay = 3000;

$(document).bind('mousemove scroll keydown', function(){ activeFlag = true; });

function update() {
  if(activeFlag) {
    doWork();
    activeFlag = false;
  }
}

window.setTimeout(update, updateDelay);

编辑:我发现代码中存在缺陷。下面的说法比较合适:

window.activeFlag = false;
window.updateDelay = 3000;

$(document).bind('mousemove scroll keydown', function(){ activeFlag = true; });

function update() {
  if(activeFlag) {
    doWork();
    activeFlag = false;
  }
  window.setTimeout(update, updateDelay);
}

update();

I think I might have ended up with something such as this. Avoids date arithmetic. Only cares whether there's been some activity since the last update().

window.activeFlag = false;
window.updateDelay = 3000;

$(document).bind('mousemove scroll keydown', function(){ activeFlag = true; });

function update() {
  if(activeFlag) {
    doWork();
    activeFlag = false;
  }
}

window.setTimeout(update, updateDelay);

edit: I've discovered a flaw in the code. The following is more appropriate:

window.activeFlag = false;
window.updateDelay = 3000;

$(document).bind('mousemove scroll keydown', function(){ activeFlag = true; });

function update() {
  if(activeFlag) {
    doWork();
    activeFlag = false;
  }
  window.setTimeout(update, updateDelay);
}

update();
倾城°AllureLove 2024-10-06 07:43:34

我认为没有简单的方法来确定用户是否存在,

我会使用 mousemovescrollkeypress 的组合。

I think there is no easy way to determine if the user is present

I would use a combination of mousemove, scroll, keypress.

榕城若虚 2024-10-06 07:43:34
var bUpdate = false;
function update() {

   if(bUpdate){
       ///perform your ajax request
   }
}

$(document).mousemove(function(){
    bUpdate = true;
    setTimeout(function(){bUpdate=false;}, 3000);}
});
var bUpdate = false;
function update() {

   if(bUpdate){
       ///perform your ajax request
   }
}

$(document).mousemove(function(){
    bUpdate = true;
    setTimeout(function(){bUpdate=false;}, 3000);}
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文