简体   繁体   English

如何清除Chrome中特定标签上的时间间隔设置?

[英]How to clear Interval set on specific tab in chrome?

I am creating a chrome extension for auto refreshing a page after the specific interval provided by the user. 我正在创建一个chrome扩展程序,以便在用户提供的特定时间间隔后自动刷新页面。 Based on the current tabs id it sets the interval and when user wishes to stop the interval he just clicks the stop button. 根据当前的标签ID,它可以设置间隔,当用户希望停止间隔时,他只需单击停止按钮即可。

I am able to setInterval and store that timers value in object for eg : javascript 我能够setInterval并将计时器值存储在对象中,例如: javascript

  var timerid = setInterval(function(){},interval)
  var timers = {
      timerid : timerid,
      tabid : tabid
  }

On Clearing Interval I just find the timer value by using the tab id 在清除间隔时,我只是使用选项卡ID查找计时器值

  var timerid = _.findWhere(timers,{tabid : tabid });
  clearInterval(timerid) 

My Background.js 我的Background.js

var tabId = 0;
var activeTabs = [];
var timers = [];

//listening for new tab message and refresh action
chrome.runtime.onMessage.addListener(   
function(request, sender, response){
    console.log("Inside background");
    console.log("Tab Id ",request.tabId)
     if(request.type === 'reload' && request.tabId !== null ){
        console.log("Tab Reload Event")
        console.log("Refreshing tab ",request.tabId );

        var first = setInterval(function(){
                                chrome.tabs.reload(request.tabId);
                             },request.interval);
        timers.push({ tabid: request.tabId,timerId : first });
        console.log("Timers");
        console.log(timers);
        console.log("Done reloading")
    }
    if(request.type === 'clear' && request.tabId !== null){

        console.log("running intervals");
        console.log(timers);
        var timer = _.findWhere(timers,{ tabid: request.tabId });
        console.log("Clearing the running interval of             ",timer.timerId);
        console.log("Tab Id ",request.tabId);
        clearInterval(timer.timerId);
    }       
}
)

chrome.tabs.onCreated.addListener(function(tab){
      activeTabs.push(tab.id);
});

But this won't clear the interval i set previously ? 但这不会清除我之前设置的时间间隔吗?

You appear to be re-declaring your timers object every time you start an interval. 每次启动间隔时,您似乎都在重新声明timers对象。

You can also avoid having to use findWhere if you used the tabid as the property name. 如果您使用findWhere作为属性名称,也可以避免使用findWhere

var timerid = setInterval(function(){},interval)
var timers[tabid] = timerid;

Then to clear you can use: 然后清除即可使用:

var timerid = timers[tabid];
clearInterval(timerid);

The other problem you may be running into is if you are declaring your timer in a tab then that won't be shared with your other tabs. 您可能遇到的另一个问题是,如果您在一个标签中声明了计时器,那么该计时器将不会与其他标签共享。 For that you will need to look at message passing , and using a background page to co-ordinate things. 为此,您需要查看消息传递 ,并使用背景页面来协调事情。

Go for setTimeout method. 选择setTimeout方法。 just do some workaround in that like below. 只需像下面那样做一些解决方法。

isStopped = false;
interval = //userinput;
var operation = function(){
  if(isStopped == true){
    return;
  }
  ......your code....
  setTimeout(operation, interval);
}

Change the global varible isStopped to true when the user wants to stop. 当用户想要停止时,将全局变量isStopped更改为true。 Do recursive call after your code. 在代码之后进行递归调用。

Let me know whether it works ? 让我知道它是否有效?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM