Reputation: 168
In a chrome extension, it is very easy to create an alarm and add a listener to it with this sample code:
chrome.alarms.create(idAlarm, {
when: dateAlarm,
periodInMinutes: parseInt(repeatAlarm)
});
chrome.alarms.onAlarm.addListener(function (alarm) {
//TODO with Listener (when alarm is activated)
)};
But, later, if I call
crome.alarms.clear(idAlarm);
The alarm is cleared but the listener is still active. What is the best way to remove a listener for a specific alarm "on the fly"?
I presume I must call chrome.alarms.onAlarm.removeListener()
but did not found a way to make this call works.
Upvotes: 1
Views: 2116
Reputation: 3488
I'm definitely not an expert regarding chrome extensions (never wrote one), but looking at the docs I think you have to name the listener function and then do it like this:
function alarmListener(alarm) {
if (alarm == "myAlarmIdentifier") {
// Do stuff.
}
}
chrome.alarms.create("myAlarmIdentifier", {
when: dateAlarm,
periodInMinutes: parseInt(repeatAlarm)
});
chrome.alarms.onAlarm.addListener(alarmListener);
Then to delete the alarm and remove the listener:
chrome.alarms.clear("myAlarmIdentifier");
chrome.alarms.onAlarm.removeListener(alarmListener);
Upvotes: 5