Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What differenciates browser.alarms.create from setTimeout/setInterval in WebExtensions?

I am creating a WebExtension for browsers. So I found out about the browser.alarms API. It basically allows you to set a (reoccurring or one-time) alarm and a callback will be fired.

Now, we've had such a feature this for a long time in JavaScript as in setTimeout and setInterval. So what is the difference to these ones? Why or in what cases may I prefer the one over the other?

I mean the main difference is obvious: You can only refire it e.g. in minutes, not seconds. Although, I think with when you can also unregister and re-register it in millisecond precision, but I think the API may be intended for longer periods, i.e. minutes. (I am just guessing here.) So why should I use it instead of a simple setInterval/setTimeout callback?

like image 512
rugk Avatar asked Nov 11 '18 15:11

rugk


People also ask

What is the difference between setTimeout and setInterval?

setTimeout: calls a function once after a particularized delay. setInterval: calls a function repeatedly, beginning after some time, then repeating continuously at the given interval. These methods are generally supported in all browsers, as well as Node.js. let timerId = setTimeout (func | code, [delay], [arg1], [arg2], ...)

What is the use of setTimeout in JavaScript?

setTimeout ( function, duration) − This function calls function after duration milliseconds from now. This goes for one execution. Let’s see an example − It waits for 2000 milliseconds, and then runs the callback function alert (‘Hello’) −

How to use alarms API in content scripts?

E.g. alarm created in background script will fire onAlarmevent in background script, options page, popup page and extension tabs (and vice versa). Alarms API is not available in Content scripts. To use this API you need to have the "alarms" permission.

What is the use of setInterval in JavaScript?

setInterval (function, duration) − This function calls function after every duration milliseconds. This goes for unlimited times. Let’s see an example − It triggers the alert (‘Hello’) after every 2000 milliseconds, not only once.


Video Answer


3 Answers

  • setTimeout/setInterval time span is limited by 2^31-1 = 2147483647 i.e. ~24 days. Values less than 0 or bigger than that are cast into int32 range, which can produce unexpected results.

  • setTimeout/setInterval is part of standard DOM, not the isolated world, so when you use it inside a content script, the web page script can clear them accidentally via clearTimeout/clearInterval.
    Workaround: post a message to the background script so it sets the timer and sends a response upon finishing.

  • Event pages (those that have "persistent": false in manifest.json) won't wait for setTimeout / setInterval before unloading due to inactivity and won't wake up for such a timer so you can only use them for a very short time (currently the event pages are guaranteed to live for 5 seconds).

Within these designated limits you can safely use setTimeout/setInterval.

like image 75
wOxxOm Avatar answered Nov 15 '22 04:11

wOxxOm


To quote from the documentation on MDN:

This is like setTimeout() and setInterval(), except that those functions don't work with background pages that are loaded on demand.

like image 32
Patrick Hund Avatar answered Nov 15 '22 03:11

Patrick Hund


In addition to what has been posted, the alarm API seems to be more reliable:

  • if you setup alarm in the past, it will fire right away
  • if you setup future date and your PC wakes up from hibernation after the date, it will fire right after waking up
  • if you setup 8 hours from now, it will fire 8 hours from now (if your PC is on) no matter how long was your PC sleeping / hibernating in the meantime

See https://discourse.mozilla.org/t/how-reliable-are-alarms/40978/8?u=rugkx, thanks to Juraj Masiar from the Mozilla Community.

like image 31
rugk Avatar answered Nov 15 '22 02:11

rugk