Chrome扩展程序:检查后台脚本是否正在运行 [英] Chrome Extension: Check if background script is running

查看:251
本文介绍了Chrome扩展程序:检查后台脚本是否正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个后台脚本,该脚本使用setInterval命令定期执行网络请求.

I have a background script that uses the setInterval command to do a network request on a routine basis.

我想知道后台脚本是否可以检测操作系统是否处于睡眠或待机状态,以便我可以调整在恢复后台脚本时显示的计时器.我了解到,根据以下答案,setInterval计时器会在睡眠期间挂起:

I was wondering if the background script can detect if the os goes on sleep or standby so that I can adjust the timer displayed upon resuming the background script. I understand that the setInterval timer is suspended during sleep based on this Answer: What happens to setTimeout when the computer goes to sleep?

代码示例为background.js

Code sample is background.js

    set_start_time();
    search_set_int = setInterval(function() {

    foo();

    // Set the Auto Search Start time for the next run 
    set_start_time();
}, frequency); // Set interval function


 var total_time_left_sec = (frequency/1000) - (current_time_unix - start_time_unix)

谢谢

推荐答案

保留带有最近一次运行间隔的时间戳的局部变量. 在正常(无睡眠)执行时,时间戳记大约是现在,但在睡眠中则要早一些,因此您知道自己来自睡眠. 也不要依赖间隔作为您的刻度"来计算经过时间 相反,请记住开始时间戳并从now()中减去

Keep a local variable with the timestamp of the last interval run. On normal (no sleep) execution, the timestamp will be about now-period, but on sleep it will be older so you know you come from a sleep. Also do not rely on the interval as your 'tick' for calculating elapsed time Instead remember the starting timestamp and substract from now()

这篇关于Chrome扩展程序:检查后台脚本是否正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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