chrome extension, execute every x minutes
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)