If you often need to do something like this, then wOxxOm is absolutely correct: The event page is a bad approach for your needs .
The documentation should not be misinterpreted: permanent background pages are by no means out of date. Event pages are a more efficient solution for background pages that process things irregularly and / or rarely.
Frequent timers do not fall into this category. After all, βexpandingβ an βEventβ page often represents a significant loss of resource / performance rather than a constant readiness.
Now the question becomes more complicated when you just need to make this timeout as part of an infrequent rather than a regular action, when you think that long pauses between these actions can benefit from the event page model. It can happen!
Then the goal becomes busy enough for Chrome, so the Event page isnβt turned off.
Probably the easiest way is to simply call the timer a little more often, since the Event page will be saved for several seconds:
var SAFE_DELAY = 1000; // Guaranteed not to fall asleep in this interval function setBusyTimeout(callback, delay) { if(delay <= SAFE_DELAY) { setTimeout(callback, delay); } else { var start = Date.now(); // setTimeout drifts, this prevents accumulation setTimeout( function() { setBusyTimeout(callback, delay - (Date.now() - start)); }, SAFE_DELAY ); } // Can be expanded to be cancellable by maintaining a mapping // of "busy timeout IDs" to real timeoutIds }
This is a kind of very "sparse" lively expectation that should not consume a lot of resources - again, if used infrequently.
Other souls may include maintaining an open port through chrome.runtime.connect and friends. I doubt it is more efficient with CPU than higher.
source share