Javascript 设置Chrome扩展中的超时范围
我的chrome扩展中有一个通知功能,由后台页面(background.html)管理 此页调用setNotificationDelay()函数,该函数基本上是删除前一个计时器,然后执行setTimeOutJavascript 设置Chrome扩展中的超时范围,javascript,google-chrome-extension,settimeout,Javascript,Google Chrome Extension,Settimeout,我的chrome扩展中有一个通知功能,由后台页面(background.html)管理 此页调用setNotificationDelay()函数,该函数基本上是删除前一个计时器,然后执行setTimeOut function setNotificationDelay() { clearNotificationTimer(); newDelay = getNotificationDelay() notificationTimer = setTimeout(showNotif
function setNotificationDelay() {
clearNotificationTimer();
newDelay = getNotificationDelay()
notificationTimer = setTimeout(showNotification, newDelay);
}
}
我还有一个options.html页面,可以配置通知时间。我想在修改配置时修改后台超时,但是当我从选项页调用setNotificationDelay()时,它不起作用,我认为这是因为计时器的作用域附加到父页
如果我让选项页打开,则会触发超时,如果我关闭它,则不会发生通知
如何将此计时器连接到背景页?
有没有更好的方法来实现它
谢谢
编辑:chrome.extension具有getBackground page方法,该方法返回后台页面的窗口对象:
function setNotificationDelay() {
clearNotificationTimer();
newDelay = getNotificationDelay()
notificationTimer = chrome.extension.getBackgroundPage().setTimeout(showNotification, newDelay);
}
}
options.js
仅在打开options.html
页面时处于活动状态,我猜您不想在等待通知时让页面保持打开状态。您(我想)想要的是让background.js
设置通知(包括background.js
和options.js
访问相同的字符串变量localStorage.yourNotificationDelay
)
需要注意的一件事是,如果您使用的不是背景页面,则不会出现超过几秒钟的通知,因为Chrome将卸载背景页面。在这种情况下,您需要使用
编辑:你的编辑让我觉得我不理解你。也许您希望用户在计时器运行时更改间隔?然后我建议使用newDelay
向background.js
发送一个消息,并使用该消息调用setNotificationDelay()