Jamie
Jamie

Reputation: 4960

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 this using jQuery by:

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!

Upvotes: 1

Views: 138

Answers (2)

RoccoC5
RoccoC5

Reputation: 4213

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.

Upvotes: 1

Andreas Eriksson
Andreas Eriksson

Reputation: 9027

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.

Upvotes: 4

Related Questions