Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chrome extension, execute every x minutes

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

like image 389
Li Song Avatar asked Dec 13 '11 13:12

Li Song


2 Answers

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.

like image 188
Xan Avatar answered Oct 07 '22 17:10

Xan


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)

like image 45
Nick Shvelidze Avatar answered Oct 07 '22 15:10

Nick Shvelidze