使用setInterval模拟setTimeout不适用于嵌套 [英] Emulate setTimeout with setInterval not working for nested
问题描述
我为Chrome编写扩展程序。当背景页面不活动时,我需要运行延迟的任务。原因setTimeout在后台选项卡中不起作用,我尝试使用setInterval模拟setTimeout,如下面的代码(位于内容脚本中):
窗口.timings = [];
函数set_timeout(func,time){
var now = new Date()/ 1;
window.timings.push({
func:func,
time:time + now
});
函数tick(){
var now = new Date()/ 1;
window.timings = window.timings.filter(function(delay_obj){
if(now> delay_obj.time){
delay_obj.func.call();
return假;
}其他{
返回true;
}
});
$ b $(函数(){
setInterval(tick,1000);
//一些代码
});
当set_interval调用延迟函数时它不工作:
set_timeout(function(){
console.log('func1');
},2000);
$ b set_timeout(function(){
console.log('func2');
set_timeout(function(){
console.log(' func3');
},3000);
},3000);
输出:
func1
func2
为什么 func3 code> not displayed?
您显然使用persistent:false 声明了chrome.com/extensions/event_pagesrel =nofollow>活动页面,它将在5几秒钟不活动。链接的文档说要使用 chrome.alarms API 。
-
如果闹钟的设置时间应该在5秒左右:
请勿使用事件页面,在manifest.json中切换到
persistent:true
。 如果警报应该设置的时间大大超过5秒:
manifest.json:
permissions:[alarms],
后台脚本:
chrome.alarms.create(MyInterval1,{when:Date.now()+ 30 * 1000});
chrome.alarms.onAlarm.addListener(function(alarm){
if(alarm.name ==MyInterval1){
console.log(Yay!) ;
chrome.alarms.create(MyInterval1,{when:Date.now()+ 30 * 1000});
}
});
另请注意:
如果事件页面关闭,其他异步HTML5 API(例如通知和地理定位)将无法完成。如果你的扩展名使用
extension.getBackgroundPage
,切换到 code> runtime.getBackgroundPage 来代替。新的方法是异步的,因此它可以在返回之前根据需要启动事件页面。
I write extension for Chrome. And I need run delayed tasks when background page inactive. Cause setTimeout not working in background tabs, I try emulate setTimeout with setInterval, like code below (located in content script):
window.timings = [];
function set_timeout(func, time){
var now = new Date() / 1;
window.timings.push({
func: func,
time: time + now
});
}
function tick(){
var now = new Date() / 1;
window.timings = window.timings.filter(function(delay_obj){
if (now > delay_obj.time){
delay_obj.func.call();
return false;
} else {
return true;
}
});
}
$(function() {
setInterval(tick, 1000);
// some code
});
And it don't work when set_interval call in delay function:
set_timeout(function(){
console.log('func1');
}, 2000);
set_timeout(function(){
console.log('func2');
set_timeout(function(){
console.log('func3');
}, 3000);
}, 3000);
Output:
func1
func2
Why func3
not displayed?
You're apparently using an event page declared with "persistent": false
in manifest.json, it is unloaded after 5 seconds of inactivity. The linked documentation says to use chrome.alarms API.
In case the alarm should be set for a period around 5 seconds:
Don't use the event page, switch to
"persistent": true
in manifest.json.In case the alarm should be set for a period much larger than 5 seconds:
manifest.json:
"permissions": ["alarms"],
background script:
chrome.alarms.create("MyInterval1", {when: Date.now() + 30*1000}); chrome.alarms.onAlarm.addListener(function(alarm) { if (alarm.name == "MyInterval1") { console.log("Yay!"); chrome.alarms.create("MyInterval1", {when: Date.now() + 30*1000}); } });
Also note:
Other asynchronous HTML5 APIs like notifications and geolocation will not complete if the event page shuts down. Instead, use equivalent extension APIs, like notifications.
If your extension uses,
extension.getBackgroundPage
, switch toruntime.getBackgroundPage
instead. The newer method is asynchronous so that it can start the event page if necessary before returning it.
这篇关于使用setInterval模拟setTimeout不适用于嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!