Chrome扩展程序 - 不到20秒后,活动页面无效 [英] Chrome Extension - event page inactive after less than 20 seconds

查看:187
本文介绍了Chrome扩展程序 - 不到20秒后,活动页面无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试创建一个Feed阅读器,每隔一小时左右检查一次Feed的更新版本。



我的问题是,事件页面似乎在少于20秒后变为非活动状态。



如何让它持续更久?

以下是代码的一些重要部分:

在manifest.json中:



 background:{
scripts:[eventPage.js],
持久性:false
},

和eventPage.js中:

$ p $ function testFunc(){
alert(test )
}
setInterval(testFunc,10 * 1000)//正在测试这里的秒数

我做错了什么?



谢谢!

p>

John

解决方案使用 setInterval 定期在后台页面上运行一些代码,但不能在活动页面,因为页面空闲时活动页面变为非活动状态很长一段时间。

在事件页面上安排定期任务的正确方法是使用 chrome.alarms API:

  chrome.alarms.onAlarm.addListener(function(alarm){
if(alarm.name =='alarm-name'){
//做某事...
testFunc();
}
});

//创建闹钟:
chrome.alarms.create('alarm-name',{
periodInMinutes:60
});


I am trying to create a feed reader that checks for an updated version of the feed every hour or so.

My problem is that the event page seems to turn inactive after less than 20 seconds.

How do I make it last longer?

Here's some of the important parts of the code:

in manifest.json:

"background": {
    "scripts": ["eventPage.js"],
    "persistent": false
},

and in eventPage.js:

function testFunc () {
    alert("test")
}
setInterval(testFunc, 10 * 1000) // was testing the number of seconds here

What am I doing wrong?

Thank you!

Regards,

John

解决方案

Using setInterval to periodically run some code works fine on background pages, but not on event pages, because the event page goes inactive when the page is idle for a long while.

The correct way to schedule a periodic task on event pages is to use the chrome.alarms API:

chrome.alarms.onAlarm.addListener(function(alarm) {
    if (alarm.name == 'name-of-alarm') {
        // Do something...
        testFunc();
    }
});

// Create the alarm:
chrome.alarms.create('name-of-alarm', {
    periodInMinutes: 60
});

这篇关于Chrome扩展程序 - 不到20秒后,活动页面无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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