I recently started developing my first Google Chrome extension and I had a problem. In my background.js script, every second I call script.js , smth like:
script.js
if (condition1) {
setTimeout(func1, 500);
result = 1;
} else
if (condition2) {
setTimeout(func2, 4000);
result = 2;
} else
result
background.js:
function func() {
chrome.tabs.executeScript(null, {file: 'script.js'},
function (result) {
console.log(result[0]);
}
);
};
var interval = null;
function onClickHandler(info, tab) {
if (info.menuItemId == "addon") {
if (interval != null) {
clearInterval(interval);
interval = null;
} else {
interval = setInterval(func, 1000);
}
}
};
chrome.contextMenus.onClicked.addListener(onClickHandler);
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({"title": "Addon", "id": "addon"});
}
I need a script call every max (1 second, after the previous execution). It should work as follows:
1. If previous script.js finished and 1 second after previous
interval call passed, call new script.js
2. If previos script.js finished and 1 second after previous
interval call haven't passed, wait for 1 second and call new script.js
3. If previous script.js haven't finished, wait until it finish
and if 1 second passed, call new script.js
Thanks in advance for your help.
source
share