I did some tests and I can confirm what @wOxxOm wrote ("calling create () will forcibly run the polling loop at the time of this created alarm")
This behavior might be acceptable,
however, when the onAlarm event trigger, the delayed alarms should be honored before the current event
Even better if they followed the right order based on the "sceduledTime" property.
Doesn't that seem reasonable to you?
Also because an alarm could modify some state variables and therefore invalidate the logic of the extension itself.
With this snippet, adapted from the @juraj one, is possible to verify that the event associated with the 'long' alarm trigger at the same time as the 'short' alarm,
but in practice the code referring to 'short' precedes that of 'long'.
(_ => {
function alarmHandler(a) {
var d1 = new Date(a.scheduledTime);
console.log('\nprogr: ' + ++idx[
a.name] + '\talarm_name: ' +
a.name + '\tscheduled at ' + d1.getHours() + ':' + ('' + d1.getMinutes()).padStart(2, '0') + ':' + ('' + d1.getSeconds()).padStart(2, '0'));
var d2 = Date.now();
console.log('The alarm is ' + (d2 - d1) + 'ms late.')
/* chrome.alarms.getAll(b => console.table(b) ) */
}
var idx = {short: 0, long: 0};
chrome.alarms.onAlarm.removeListener(alarmHandler);
chrome.alarms.onAlarm.addListener(alarmHandler);
chrome.alarms.clearAll(async _ => {
var d = new Date();
console.log('Now is: ' + d.getHours() + ':' + ('' + d.getMinutes()).padStart(2, '0') + ':' + ('' + d.getSeconds()).padStart(2, '0'));
await chrome.alarms.create('long', {when: Date.now() + 180 * 1000, periodInMinutes: 3});
await chrome.alarms.create('short', {when: Date.now() + 30 * 1000, periodInMinutes: 1});
chrome.alarms.getAll(a => {
a.forEach(v => {
var d = new Date(v.scheduledTime);
console.log('Alarm_name: ' +
v.name + '\tScheduled at: ' + d.getHours() + ':' + ('' + d.getMinutes()).padStart(2, '0') + ':' + ('' + d.getSeconds()).padStart(2, '0'))
})
})
})
})()