【发布时间】:2012-12-15 14:40:10
【问题描述】:
当使用 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。
如何做到这一点?
【问题讨论】:
标签: javascript google-chrome javascript-events google-chrome-extension alarm