Google Chrome 拡張アラームを使用している場合、アラームが設定されている場合はアラームが鳴り、アラームの時間が経過すると Chrome が閉じられ、再度開かれます。
どうすればこれを止めることができますか?
これは、私が何を意味するかを説明するための小さなコード サンプルです。
/*
If we perform Browser Action to create alarm, then
close the browser, wait about 2 minutes for the alarm to expire
and then reopen the browser, the alarm will go off and the DoSomething
function will get called twice, once by the onStartup event and once
by the onAlarm event.
*/
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.alarms.create('myAlarm', {
delayInMinutes : 2.0
});
});
chrome.alarms.onAlarm.addListener(function (alarm) {
console.log('Fired alarm!');
if (alarm.name == 'myAlarm') {
createListener();
}
});
chrome.runtime.onStartup.addListener(function () {
console.log('Extension started up...');
DoSomething();
});
function DoSomething() {
alert('Function executed!');
}
したがって、私のコード サンプルの上部にあるコメントを読めば、何が起こるかがわかります。
私が欲しいのは、ブラウザが起動されたばかりの場合にイベントDoSomething
によってのみ機能が実行されるように、ブラウザが閉じられた場合にアラームがクリアされ、ブラウザが起動された後にのみアラームが機能を実行できるようにすることです。私のコードは新しいアラームを作成します。onStartup
DoSomething
ブラウザを閉じた後もアラームが鳴り続けonAlarm
て、ブラウザが再度開いたときに実行されることは決してありません。
どうすればこれを達成できますか?