我需要知道如何为同一个函数设置2个时间间隔?我的意思是,现在我设置了1秒的时间间隔来连续监视服务器文件的输出。如果服务器文件的输出是0,那么颜色扩展图标的变化,它显示一个通知。现在的问题是,我已经在同一个功能中写了这两个功能。因为我设置了1秒的时间间隔并调用该功能,通知显示每1秒和颜色图标也会根据服务器输出每1秒更改一次,这很好。现在我需要的是我需要每1秒更换一次颜色。但是我需要每隔5分钟显示一次通知。请你帮帮我。已发布我的background.js.can你能帮帮我吗? 这是我的background.js
var myNotificationID = null;
var oldChromeVersion = !chrome.runtime;
var interval = 5 * 60 * 1000; // 5 minutes in milliseconds
var lastNotification = 0;
setInterval(function() {
updateIcon();
}, 1000);
function getGmailUrl() {
return "http://calpinemate.com/";
}
function isGmailUrl(url) {
return url.indexOf(getGmailUrl()) == 0;
}
function onInit() {
updateIcon();
if (!oldChromeVersion) {
chrome.alarms.create('watchdog',{periodInMinutes:5,delayInMinutes: 0});
}
}
function onAlarm(alarm) {
if (alarm && alarm.name == 'watchdog') {
onWatchdog();
}
else {
updateIcon();
}
function onWatchdog() {
chrome.alarms.get('refresh', function(alarm) {
if (alarm) {
console.log('Refresh alarm exists. Yay.');
}
else {
updateIcon();
}
});
}
if (oldChromeVersion) {
updateIcon();
onInit();
}
else {
chrome.runtime.onInstalled.addListener(onInit);
chrome.alarms.onAlarm.addListener(onAlarm);
}
function updateIcon(){
if(localStorage.username){
var urlPrefix = 'http://www.calpinemate.com/employees/attendanceStatus/';
var urlSuffix = '/2';
var req = new XMLHttpRequest();
req.addEventListener("readystatechange", function() {
if (req.readyState == 4) {
if (req.status == 200) {
var item=req.responseText;
if(item==1){
chrome.browserAction.setIcon({path:"calpine_logged_in.png"});
chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]});
chrome.browserAction.setBadgeText({text:""});
chrome.notifications.clear('id1', function(){});
}
else{
chrome.browserAction.setIcon({path:"calpine_not_logged_in.png"});
chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]});
chrome.browserAction.setBadgeText({text:""});
var now = new Date().getTime();
if (now - lastNotification > interval) {
chrome.notifications.create(
'id1',{
type: 'basic',
iconUrl: '/calpine_not_logged_in.png',
title: 'Warning : Attendance',
message: 'Please mark your Attendance !',
buttons: [{ title: 'Mark',
iconUrl: '/tick.jpg'
},{ title: 'Ignore',
iconUrl: '/cross.jpg'}],
priority: 0},
function(id) { myNotificationID = id;}
);
}
}
}
else {
alert("ERROR: status code " + req.status);
}
}
});
var url = urlPrefix + encodeURIComponent(localStorage.username) + urlSuffix;
req.open("GET", url);
req.send(null);
}
}
onInit();
答案 0 :(得分:1)
一种可能的方法是跟踪上次通知显示的时间,并始终检查是否已经过了5分钟。 E.g:
/* Put these 2 lines at the very top of your script */
var interval = 5 * 60 * 1000; // 5 minutes in milliseconds
var lastNotification = 0;
然后,在updateIcon()
函数内,替换此行:
chrome.notifications.create(...);
这些行:
var now = new Date().getTime();
if (now - lastNotification > interval) {
lastNotification = now;
chrome.notifications.create(...);
}
如果自上次创建通知起已经过了5分钟,上面的代码将确保通知仅创建 。它还会使用当前时间更新lastNotification
变量。
答案 1 :(得分:0)
拥有此代码,每隔60秒运行一次功能,并在功能
之间运行var launchTime = undefined;
function Launcher() {
if (undefined == launchTime) {
launchTime = new Date();
}
nowTime = new Date();
diff = (nowTime.getTime() - launchTime.getTime()) / 1000;
if (diff % 60 == 0 and diff > 0) { // 60 seconds have passed
functionA();
} else { // 1 second has passed
functionB();
}
}
function functionA() {
}
function functionB() {
}
setInterval(function() { Launcher(); }, 1000);