【问题标题】:How to keep a function running in background.js even when popup.html is out of view即使 popup.html 不在视野范围内,如何保持函数在 background.js 中运行
【发布时间】:2022-06-16 00:33:06
【问题描述】:

即使 popup.html 关闭,如何保持此计时器运行?

我将最后一次保存在存储中,以便它从上次停止点恢复 但是如何保持 setinterval 继续运行 清单有 坚持为真。这个脚本在后台

function timer(){
  if(localStorage.getItem('tile')==null){
    secs=32400;
    }
    
    if(localStorage.getItem('tile')!=null){
      secs=localStorage.getItem('tile');
    }
    console.log(secs);
    if(secs!==0){
     
    function z(n) { return (n < 10 ? '0' : '') + n; }
    var sign = secs < 0 ? '-' : '';
    secs = Math.abs(secs);
    secs = secs-1;
    


chrome.storage.local.get(["actualtime"],function (result){aht = (((Number(result.actualtime))*((13/8)*60))/(28800-secs)/60);
document.getElementById("time2").innerHTML = aht.toFixed(2);
    })
    aht2 = (((secs/60))*((13/8)))/(28800/60);
    document.getElementById("time3").innerHTML = aht2.toFixed(2);
    document.getElementById("counter").innerHTML= sign + z(secs / 3600 | 0) + ':' + z((secs % 3600) / 60 | 0) + ':' + z(secs % 60);
    console.log(localStorage.getItem('actualtime'));
   
    localStorage.setItem('tile',secs);
}

}

setInterval(timer,1000);

【问题讨论】:

    标签: javascript


    【解决方案1】:

    背景和popup.html不能使用相同的js //我犯的错误 为背景运行了一个单独的 js,为 popup.html 运行了一个 js 使用 msg 解析在它们之间进行通信 后台一直在运行,只有当 popup.html 打开时才会调用 popup.html 的 js。(关闭时停止)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 2019-07-30
      • 2012-07-19
      • 1970-01-01
      相关资源
      最近更新 更多