I'm just making a simple chrome extension.
I want my background page(or part of ) to execute every 5 minutes, to get some data and display a desktop notification if any.How can I do this
Important note: if you make an extension with an Event page ("persistent": false
in the manifest), setInterval
with 5-minute interval will fail as the background page will get unloaded.
If your extension uses window.setTimeout() or window.setInterval(), switch to using the alarms API instead. DOM-based timers won't be honored if the event page shuts down.
In this case, you need to implement it using the chrome.alarms
API:
chrome.alarms.create("5min", {
delayInMinutes: 5,
periodInMinutes: 5
});
chrome.alarms.onAlarm.addListener(function(alarm) {
if (alarm.name === "5min") {
doStuff();
}
});
In case of persistent background pages, setInterval
is still an acceptable solution. It should also work for short (on a scale of seconds, not minutes) intervals in an event page, but it will keep it from unloading, negating the benefits.
One way to accomplish this would be:
setInterval(your_function, 5 * 60 * 1000)
Which would execute your_function
every 5 minutes (5 * 60 * 1000 milliseconds = 5 minutes)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With