0

我有一个问题,我还不能解决。我有一个带有菜单和选项卡的弹出页面,还有一个设置选项卡。在设置选项卡上,我一些项目保存到本地存储,其中之一是用于桌面通知的 notification_time。

注意:我没有选项页面!

我的扩展有这个弹出窗口和一个背景页面,它的功能是通过桌面通知提醒用户。我每隔 5,10,30 分钟,1,2 小时等显示一次通知。这个时间应该可以在弹出页面的选项菜单中选择。问题是,如果保存了 5 分钟,例如当我更新到 10 分钟时,background.html 并没有更新自己!我几乎重写了 20 次代码,但找不到解决方案。这是一个代码示例和一个打印屏幕,可以清楚地了解我的问题。

在此处输入图像描述

弹出

$("#save_settings").click(function(){
        var bgp = chrome.extension.getBackgroundPage();
        localStorage.setItem("notifynumber",$("#notifynumber").val());

        if($("#notify").attr('checked')){
            localStorage.setItem('chbox','true');
        } else {
            localStorage.setItem('chbox','false');
        }

        if($("#notif_time_select :selected").val()!="default"){
            bgp.setTime(parseInt($("#notif_time_select :selected").val()));
        }

        if($("#org_select :selected").val()!="default"){
            localStorage.setItem('org',$("#org_select :selected").val().replace(/%20/g," "));
        }
    });

注意:save_settings 是一个按钮,在选项卡上有一个复选框(如果选中则允许通知,否则禁用)。有两个 html 选择标签,一个用于选择一些数据(org = 组织),一个用于选择时间。"#notif_time_select"是 html 选择标签,我选择 5,10,30 分钟等...

所以,每当我点击保存按钮时,我都会将复选框状态保存到本地存储,并从后台页面调用一个函数,以节省时间。:bgp.setTime(parseInt($("#notif_time_select :selected").val()));

背景页面:

为了节省时间,我使用函数 setTime:

var time = 300000; // default
function setTime(time){
    this.time=time;
    console.log("time set to: "+this.time);
}

之后,我使用 setInterval 定期显示通知

setInterval(function(){         
                howmanyOnline("notify",function(data){
                    if(data>localStorage.getItem("notifynumber")){
                        if (!window.webkitNotifications) { // check browser support
                            alert('Sorry , your browser does not support desktop notifications.');
                        }
                        notifyUser(data); // create the notification
                    }
                    if(localStorage.getItem('tweet')=='true'){
                        if(data>localStorage.getItem("notifynumber")){
                            sendTweet(data,localStorage.getItem('org'),localStorage.getItem('ck'),localStorage.getItem('cs'),localStorage.getItem('at'),localStorage.getItem('ats'));
                        }
                    }
                });
    },time);

setInterval 中的代码工作正常,唯一的问题是,

},time);

更新不好。如果我更改设置以每 10 分钟显示一次通知,它会保持 5 分钟。唯一的方法是重新启动整个扩展。如何在不重新启动整个扩展程序的情况下更新 setInterval 的频率?谢谢吉姆


如果我也将 notif_time 保存到 localStorage 会怎样,并且在后台设置一个监听器来监听 localStorage 的变化。有没有办法监听特定的 localStorage 项目的变化?!

4

2 回答 2

1

现在,setInterval仅在您的应用程序加载时运行一次。如果您希望间隔以新的时间间隔触发,您应该使用clearInterval然后重新调用setInterval.

// set a new time, wipe out the old interval, and set a new interval
function setTime(t) {
    window.time = t;
    clearInterval(notifInterval);
    notifInterval = setInterval(makeNotification, time);
}

// set first interval
notifInterval = setInterval(makeNotification, time);

function makeNotification() {
    // do what you need to make a notification
}

这里,notifInterval是对区间的引用,由 返回setInterval,用于清除它。

于 2012-05-09T15:04:53.823 回答
0

您问题中的源代码未完成,但我猜您调用了 setInterval() ,然后在后台页面中修改了 window.time 。
您的 window.time 不是一个对象,而是一个数值,并且 setInterval() 在调用后无法“看到” window.time 的变化。

尝试这个:

function onTimeout() {
    // do your notification.

    setTimeout(onTimeout, time);
}

setTimeout(onTimeout, time);
于 2012-05-09T16:15:30.483 回答