开发者

Always up to date content using AJAX

At the moment I am using ajax requests every 10 minutes to update certain content and other time intervals for others.

I do thi开发者_开发问答s using jQuery by:

  • On mouse move, the active page is checked
  • If the active page has not be updated within the given time interval, the page is updated

I'm doing this because although i want the content to stay up to date, I don't want it to be sending requests in the background (when the user is not using the application). Doing this also means that if the user has not used it for more than the time period, when they start to use it again it will automatically update.

I'm wondering just how efficient this is as whenever the mouse moves the checks are called (and has slowed down performance a bit - especially when trying to click links) - is the a more efficient way to do this?

Thanks!


I would rather activate/reset a timer, on say, 60 seconds, on movement of the mouse, and set your fixed-interval checks to only run if that timer is above zero.

That way, checks aren't made every time the mouse moves, and if the user becomes inactive, update checks stop after 60 seconds.


Another possible solution would be to use the window blur and focus events to determine if the window is active:

var checkForContentUpdates = true;

$(window).focus(function() {
    checkForContentUpdates = true;
});

$(window).blur(function() {
    checkForContentUpdates = false;
});

Your AJAX routine would then key off of the checkForContentUpdates bool.

I'm sure that there are scenarios where this isn't fool-proof, so you'd likely have to combine this method with other logic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜