javascript Chrome 扩展 setTimeout 无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26539801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome extension setTimeout not working properly
提问by badcom
My first post here =].
我在这里的第一篇文章 =]。
I'm building a chrome extension and I'm using a setTimeout recursively. I noticed that if I set it to up to 13secs, it works, but if I set it to 14secs+ it won't work.
我正在构建一个 chrome 扩展,并且我正在递归地使用 setTimeout。我注意到如果我将它设置为最多 13 秒,它可以工作,但如果我将它设置为 14 秒+,它将不起作用。
This is an example which is on my background.js
这是我的 background.js 上的一个例子
function start() {
var timeout = setTimeout( function() { start(); }, 1000*15);
alert('test');
}
chrome.webNavigation.onCompleted.addListener(function(o) {
start();
}, {
url: [
{urlContains: 'http://www.example.com/in.php'},
{urlContains: 'http://www.example.com/out.php'}
]
}
);
If you reduce that timeout to 1000*13, it works.
如果您将该超时减少到 1000*13,它就可以工作。
This is my manifest.json
这是我的 manifest.json
{
"name": "Extension",
"version": "0.0.7",
"manifest_version": 2,
"description": "Keeps proxy session alive",
"homepage_url": "http://www.example.com",
"icons": {
"16": "icons/icon16-on.png",
"48": "icons/icon48-on.png",
"128": "icons/icon128-on.png"
},
"default_locale": "en",
"background": {
"scripts": [
"src/bg/background.js"
],
"persistent": false
},
"browser_action": {
"default_icon": "icons/icon19.png",
"default_title": "Example - Off",
"default_popup": ""
},
"permissions": [
"webNavigation",
"*://*/*",
"https://*/*"
]
}
Any idea on what could be causing this oddness? I'm testing this on developer mode, BTW.
关于什么可能导致这种奇怪的想法?我正在开发人员模式下测试这个,顺便说一句。
Thanks in advance!
提前致谢!
EDIT
编辑
Code fixed:
代码固定:
manifest.json
清单文件.json
I added "alarms" to the permissions
我在权限中添加了“警报”
background.js
背景.js
Added this event to listen to the alarms.create:
添加了这个事件来监听alarms.create:
chrome.alarms.onAlarm.addListener(function(alarm){
start();
});
Replaced the setTimeout function with the below line
用下面的行替换了 setTimeout 函数
chrome.alarms.create("Start", {periodInMinutes:1});
Hope this helps!
希望这可以帮助!
回答by Brian Henry
I suspect the problem may with the automatic suspension of event pages after some period of inactivity. On my machine, onSuspend
seems to called after ~10 seconds.
我怀疑问题可能与在一段时间不活动后自动暂停事件页面有关。在我的机器上,onSuspend
似乎在大约 10 秒后调用。
https://developer.chrome.com/extensions/event_pages#lifetimenotes that
https://developer.chrome.com/extensions/event_pages#lifetime指出
Once the event page has been idle a short time (a few seconds), the runtime.onSuspend event is dispatched. The event page has a few more seconds to handle this event before it is forcibly unloaded.
一旦事件页面空闲了一小段时间(几秒钟),就会调度 runtime.onSuspend 事件。事件页面在强制卸载之前还有几秒钟的时间来处理此事件。
So, that may get you more around 13 seconds before the page is actually unloaded (giving you some cleanup time in onSuspend, I reckon). Then your page is unloaded and code initiated from there is no longer run.
因此,这可能会让您在实际卸载页面前 13 秒获得更多时间(我认为在 onSuspend 中为您提供了一些清理时间)。然后您的页面被卸载并且从那里启动的代码不再运行。
https://developer.chrome.com/extensions/event_pages#transitionsays to use the alarms apifor event pages instead of setTimeout
.
https://developer.chrome.com/extensions/event_pages#transition说对事件页面使用警报 api而不是setTimeout
.