-1

I'm building an interactive page where a chatbox and a list of items appear.

I intend to update both of them every 30 seconds using JQUERY's .post() function, but I realize many users keep webpages opened in tabs 24 hours a day, creating a heavy load on the server.

In order to prevent this and to optimize my ressources, I would like the database polling to be executed every 30 seconds ONLY if the user is active on the page; in the passive cases, I would like a 5 minutes polling.

How can I achieve this? (I'm using PHP and MYSQL)

WooCaSh
  • 5,180
  • 5
  • 36
  • 54
Adam Strudwick
  • 12,671
  • 13
  • 31
  • 41

2 Answers2

0

Perhaps you can create a function that's based on the onScroll event? But I suppose that depends on what your definition of 'active' is.

Shane Duff
  • 21
  • 6
0

Maybe try detect cursor position changing.

  1. On page load start setInterval method to send request to PHP
  2. Add adittional setInterval to check if user cursor is moving
  3. If not wait some time and then clear 1 interval.
  4. If yes allow interval works normally.

I'm not sure how will work cursor position checking method if user go to other tab. I will prepare test for you (only js)

EDIT:

W3C actually start working on page visibility as document attribute: http://www.w3.org/TR/page-visibility/

But for now is better solution. Look on this answer: https://stackoverflow.com/a/1060034/390747

Community
  • 1
  • 1
WooCaSh
  • 5,180
  • 5
  • 36
  • 54