Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a user is actively viewing a webpage

Tags:

html

I have a webpage that updates itself in AJAX every 5 seconds or so by fetching data from a server. I would like to, however, only perform the update when the user is actively viewing the page.

When the user is on another tab, in another program other than the browser, or any other time the page is not the main active page on the user's PC, the update should not take place.

What would be the most accurate, cross-browser way to test this?

like image 704
user1094786 Avatar asked Oct 28 '25 01:10

user1094786


2 Answers

The jQuery idleTimer plugin should do exactly what you need.

Docs are here: http://paulirish.com/2009/jquery-idletimer-plugin/

Demo is here: http://ajaxdump.com/?8ADKBT6O

like image 186
Anna_MediaGirl Avatar answered Oct 31 '25 11:10

Anna_MediaGirl


Best to use the Page Visibility API: https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API

like image 35
dougwig Avatar answered Oct 31 '25 12:10

dougwig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!