Chrome extension, execute every x minutes

I'm just doing a simple chrome extension.

I want my background page (or part) to run every 5 minutes in order to get some data and display a notification on the desktop, if any. How can i do this

+4
source share
2 answers

One way to achieve this:

setInterval(your_function, 5 * 60 * 1000) 

Executing your_function every 5 minutes (5 * 60 * 1000 milliseconds = 5 minutes)

+5
source

Important note: if you create an extension with an Event Page ( "persistent": false in the manifest), setInterval with a 5-minute interval will fail because the background page will be unloaded.

If your extension uses window.setTimeout () or window.setInterval (), switch to using the crash API instead. DOM-based timers will not run if the event page turns off.

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 the case of constant background pages, setInterval is still an acceptable solution.

+9
source

Source: https://habr.com/ru/post/1386109/


All Articles