简体   繁体   English

Chrome扩展程序中的setTimeOut范围

[英]setTimeOut scope in a Chrome extension

I have a notification functionality in my chrome extension which is managed by a background page (background.html) This page calls the setNotificationDelay() function which is basically a deletion of the previous timer followed by a setTimeOut. 我的Chrome扩展程序中有一个通知功能,由后台页面管理(background.html)此页面调用setNotificationDelay()函数,该函数基本上是先前计时器的删除,后跟setTimeOut。

function setNotificationDelay() {
    clearNotificationTimer();
    newDelay = getNotificationDelay()
    notificationTimer = setTimeout(showNotification, newDelay);
    }
}

I have also an options.html page which allows the configuration of the notification hour. 我还有一个options.html页面,允许配置通知小时。 I want to modify the background timeout when the configuration is modified, but when I call the setNotificationDelay() from the Option page it doesn't work, and I presume it's because the scope of the timers are attached to the parent page. 我想在修改配置时修改后台超时,但是当我从Option页面调用setNotificationDelay()时它不起作用,我认为这是因为定时器的范围附加到父页面。

If I let the Option page opens the timeout is triggered, if I close it the notification never happened. 如果我打开选项页面,则会触发超时,如果我关闭它,通知就不会发生。

How can I attach this timer to the background page ? 如何将此计时器附加到后台页面? Is there a better way to achieve it ? 有没有更好的方法来实现它?

Thanks. 谢谢。

EDIT: The chrome.extension have the getBackground page method which returns the window object for the background page: 编辑:chrome.extension具有getBackground页面方法,该方法返回后台页面的窗口对象:

function setNotificationDelay() {
    clearNotificationTimer();
    newDelay = getNotificationDelay()
    notificationTimer = chrome.extension.getBackgroundPage().setTimeout(showNotification, newDelay);
    }
}

options.js is only active while the options.html page is open, and I presume you don't want to leave the page open while you're waiting for the notification. options.js仅在options.html页面打开时才有效,我认为您在等待通知时不想让页面保持打开状态。 What you want (I think) is to have background.js set the notification (both background.js and options.js access the same string variable localStorage.yourNotificationDelay ). 你想要的(我认为)是让background.js设置通知( background.jsoptions.js访问相同的字符串变量localStorage.yourNotificationDelay )。

One thing to watch out for is that if you are using event pages instead of background pages, a notification of more than a few seconds won't happen because Chrome will unload the background page. 需要注意的一件事是,如果您使用的是事件页面而不是背景页面,则不会发生超过几秒钟的通知,因为Chrome将卸载后台页面。 In that case, you'll need to use alarms instead. 在这种情况下,您需要使用警报

Edit: Your edit makes me think I'm not understanding you. 编辑:你的编辑让我觉得我不理解你。 Perhaps you're expecting the user to change the interval while the timer is running? 也许您希望用户在计时器运行时更​​改间隔? Then I'd recommend sending a message to background.js with newDelay , and use that to call setNotificationDelay() . 然后我建议使用newDelaybackground.js发送消息 ,并使用它来调用setNotificationDelay()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM