Created
August 25, 2011 01:18
-
-
Save vroy/1169734 to your computer and use it in GitHub Desktop.
Jetpack - How to stop a contentscript setInterval?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.on("message", function(tab_id) { | |
console.log("Starting loop for: "+tab_id); | |
setInterval(function() { | |
console.log( "interval "+tab_id+": "+(new Date()).getTime() ); | |
}, 1000); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
It seems that the content script of a page-mod continues to execute even when a tab is closed/detached. | |
As you can see in this example output, I opened two tabs. Every second the content script outputs the tab_id received and the current time. I only closed the second tab. I have two problems: | |
1 - I don't understand why the "Detached a tab" message appeared twice while only one tab was closed. Anybody can explain this behavior or confirm that it's a bug? | |
2 - The two intervals that were started in the content script continued to run. I expected that the content script would stop executing completely when a tab was closed/detached. How do I make sure that the content script stops executing completely when a tab is closed/detached? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pageMod = require("page-mod"); | |
var data = require("self").data; | |
var mod = pageMod.PageMod({ | |
include: "*", | |
contentScriptWhen: "ready", | |
contentScriptFile: [ data.url("contentscript.js") ], | |
onAttach: function(worker) { | |
worker.postMessage(worker.tab.index); | |
worker.on("detach", function() { | |
console.log("Detached a tab."); | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
info: Starting loop for: 0 | |
info: interval 0: 1314234077833 | |
info: interval 0: 1314234078834 | |
info: interval 0: 1314234079836 | |
info: interval 0: 1314234080835 | |
info: Starting loop for: 1 | |
info: interval 0: 1314234081828 | |
info: interval 1: 1314234081915 | |
info: interval 0: 1314234082830 | |
info: interval 1: 1314234082916 | |
info: interval 0: 1314234083848 | |
info: interval 1: 1314234083909 | |
info: Detached a tab. | |
info: Detached a tab. | |
info: interval 0: 1314234084847 | |
info: interval 1: 1314234084898 | |
info: interval 0: 1314234085843 | |
info: interval 1: 1314234085896 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment