chrome.idle:如何查询当前检测间隔?

时间:2016-04-26 14:56:38

标签: google-chrome

你可以这样做:

chrome.idle.setDetectionInterval(60*60);
chrome.idle.onStateChanged.addListener(my_code)

然而,浏览器控制台上的某人可以输入:

chrome.idle.setDetectionInterval(15);

...这会影响调用my_code的方式。这对于调试很有用,但它引出了一个问题:如何查询当前检测间隔?

1 个答案:

答案 0 :(得分:1)

查询检测间隔是没有办法(至少我或谷歌都不知道)。但是我们知道默认值是60秒,并且可以覆盖该功能。 (您只需要确保在您覆盖该功能之前没有人在您的应用环境中调用setDetectionInveral - 如果您无法确定,则必须在之后手动将检测间隔设置为已知值。)

实施例:

var currentDetectionInterval = 60;
var originalSetDetectionInterval = chrome.idle.setDetectionInterval;
chrome.idle.setDetectionInterval = function(detectionIntervalInSeconds, callback) {
    currentDetectionInterval = detectionIntervalInSeconds;
    return originalSetDetectionInterval.apply(this, arguments);
};

// Optional, if you like this.
chrome.idle.getDetectionInterval = function() {
    return currentDetectionInterval;
};

这将允许您通过检查变量currentDetectionInterval来查询检测间隔,或者如果您更喜欢这样,可以使用我们刚刚添加的新函数chrome.idle.getDetectionInterval

相关问题