DEV Community

Discussion on: How to Detect Idle Browser Tabs with JavaScript

Collapse
 
jalners profile image
Alexey Neretin

Thanks for this article. I would like to propose a little fix.

Try to do next:

  • Add your Example with Polling on the page.
  • Reload the page and switch a browser tab before your sample will be reloaded.
  • Wait until your page with sample will be reloaded.
  • Go back to your page.
  • You will see that poller calls console.log twice as often.
  • Try to stop the poller. It will work even after poller.stop() call.

This is due to double call of the poller.start. For the first time from your sample code. The second time you call it from the onVisibilityChange. There was no poller.stop method call between double poller.start method call in this case.

You can avoid it by calling the poller.stop method before the poller.start in the onVisibilityChange.