admin管理员组

文章数量:1332359

I am developing a browser extension for a real-time product. I have a background page with "persistent : true" set in the manifest.json (I am using version v2). I am continually polling the server for new data every second using setInterval(). The background script also caches the data it has gathered till present and gives it to any newly opened tab.

Things work fine until sometimes I noticed that when I put the puter to sleep for a long period, my poll to server just stops! If I refreshed any of the existing tabs, I do see cached data. This means, that the background page was not killed by Chrome. My question is, why is chrome just stopping the setInterval() call? Also, what is the correct way to revive the poll if it's stopped for some reason?

//relevant part of manifest.json
  "background": {
    "scripts": [
      "js/background/jquery.min.js",
      "js/background/bgconfig.js",
      "js/background/backgroundmanager.js",
      "js/background/eventsfetcher.js"
    ],

    "persistent": true
  },

Thanks!

I am developing a browser extension for a real-time product. I have a background page with "persistent : true" set in the manifest.json (I am using version v2). I am continually polling the server for new data every second using setInterval(). The background script also caches the data it has gathered till present and gives it to any newly opened tab.

Things work fine until sometimes I noticed that when I put the puter to sleep for a long period, my poll to server just stops! If I refreshed any of the existing tabs, I do see cached data. This means, that the background page was not killed by Chrome. My question is, why is chrome just stopping the setInterval() call? Also, what is the correct way to revive the poll if it's stopped for some reason?

//relevant part of manifest.json
  "background": {
    "scripts": [
      "js/background/jquery.min.js",
      "js/background/bgconfig.js",
      "js/background/backgroundmanager.js",
      "js/background/eventsfetcher.js"
    ],

    "persistent": true
  },

Thanks!

Share Improve this question asked Aug 8, 2013 at 21:17 Trunal BhanseTrunal Bhanse 1,6911 gold badge17 silver badges28 bronze badges 8
  • 2 Chrome isn't stopping it. Your OS is. – Travis J Commented Aug 8, 2013 at 21:19
  • 1 @TravisJ: Thanks but what is the best way of reviving it? – Trunal Bhanse Commented Aug 8, 2013 at 21:20
  • 1 Store the intervalID returned from setInterval in a global variable, then when the window gains focus again, clear that interval and run setInterval again. It seems there isn't really a way to check if intervals are still running, so that's why I'm suggesting that. Here's a link with information about checking if the window's gained focus: stackoverflow./questions/2720658/… – Pluto Commented Aug 8, 2013 at 21:38
  • 1 @Pluto: That was my last resort but seems like I will have to go with that. Thanks for your help! – Trunal Bhanse Commented Aug 8, 2013 at 22:38
  • 1 Realtime? Have you considered using Websockets to reduce the network load? If not, at least use the chrome.idle API to detect that the user went away, in order to save resources when the user isn't there – Rob W Commented Aug 9, 2013 at 8:51
 |  Show 3 more ments

2 Answers 2

Reset to default 6

According to the chrome documentation you should use the alarms API instead. I don't know if it will solve the issue but it's definitively worth trying!

I quote:

If your extension uses window.setTimeout() or window.setInterval(), switch to using the alarms API instead. DOM-based timers won't be honoured if the event page shuts down.

https://web.archive/web/20130715023501/http://developer.chrome./extensions/event_pages.html

=> https://developer.chrome./docs/extensions/reference/alarms/

For anyone else looking for the solution, I ended up implementing what @Pluto suggested :

"Store the intervalID returned from setInterval in a global variable, then when the window gains focus again, clear that interval and run setInterval again"

This has worked well for me so far with no dropped events from server.

本文标签: javascriptsetInteval in background scriptStack Overflow