Chrome扩展程序setTimeout无法正常工作 [英] Chrome extension setTimeout not working properly

查看:367
本文介绍了Chrome扩展程序setTimeout无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一篇文章=]。



我正在构建chrome扩展,并且正在递归地使用setTimeout。我注意到,如果我将它设置为13secs,它可以工作,但是如果我将它设置为14secs +,它将无法工作。



这是一个示例my background.js

  function start(){

var timeout = setTimeout(function(){ start();},1000 * 15);
alert('test');


chrome.webNavigation.onCompleted.addListener(函数(o){

start();

},{
网址:[
{urlContains:'http://www.example.com/in.php'},
{urlContains:'http://www.example.com/out。 php'}
]
}
);

如果您将该超时降低到1000 * 13,则可以使用。



这是我的manifest.json

  {
name:Extension,
version:0.0.7,
manifest_version:2,
description:让代理会话保持活跃状态​​,
homepage_url:http: //www.example.com,
图标:{
16:图标/ icon16-on.png,
48:图标/图标48-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:示例 - 关,
default_popup:
},
权限:[
webNavigation,
*:// * / *,
https:// * / *
]
}

有什么想法可能会导致这种奇怪吗?我在测试开发者模式,顺便说一句。



感谢提前!

编辑



固定代码:

manifest.json



我向权限添加了警报。
$ b

background.js



添加此事件来收听alarms.create:

  chrome.alarms.onAlarm。 addListener(function(alarm){
start();
});

用下面的行代替setTimeout函数

  chrome.alarms.create(Start,{periodInMinutes:1}); 

希望这有助于您!

解决方案

我怀疑这个问题可能会在事件页面处于不活动状态后自动暂停。在我的机器上, onSuspend 似乎在约10秒后调用。



https://developer.chrome。 com / extensions / event_pages#lifetime 注意到


一旦事件页面在短时间内(几秒钟) ,将调度
runtime.onSuspend事件。事件页面在被强制卸载之前还有几个b $ b秒的时间来处理这个事件。


所以,你实际上已经卸载了13秒左右的时间(给你一些onSuspend的清理时间,我估计)。然后你的页面被卸载,并且从那里启动的代码不再运行。



https://developer.chrome.com/extensions/event_pages#transition 说要使用报警api 为事件页面而不是 setTimeout


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.

This is an example which is on my 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.

This is my 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

I added "alarms" to the permissions

background.js

Added this event to listen to the alarms.create:

chrome.alarms.onAlarm.addListener(function(alarm){
    start();
});

Replaced the setTimeout function with the below line

chrome.alarms.create("Start", {periodInMinutes:1});

Hope this helps!

解决方案

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.

https://developer.chrome.com/extensions/event_pages#lifetime notes that

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.

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.

https://developer.chrome.com/extensions/event_pages#transition says to use the alarms api for event pages instead of setTimeout.

这篇关于Chrome扩展程序setTimeout无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆