Reputation: 4272
I am creating an extension for Firefox. This includes using self.port
. Here's my code in panel.js
:
self.port.on("showDlg", function(val) {
document.getElementById('queen').onclick = function(){
self.port.emit("song", 'queen');
};
document.getElementById('beatles').onclick = function(){
self.port.emit("song", 'beatles');
};
});
Here's the content.js
:
self.port.on("song", function(val) {
if (val.code = 'queen'){
interval = setInterval(function () {
console.log('show must go on');
}, 1000);
} else if (val.code == 'beatles'){
interval = setInterval(function () {
console.log('yesterday');
}, 1000);
}
}
It is all working, when I click queen
, it prints me show must go on
every 1 second. But when I click beatles
, it still prints show must go on
along with yesterday
.
How can I stop previous interval? As far as I understand, it runs in the background, and every action is a new instance. If so, how can I stop previous instance?
Thanks.
Upvotes: 1
Views: 102
Reputation: 422
self.port.on("song", function(val) {
self.interval && clearInterval(self.interval);
self.interval = null;
if (val.code = 'queen'){
self.interval = setInterval(function () {
console.log('show must go on');
}, 1000);
} else if (val.code == 'beatles'){
self.interval = setInterval(function () {
console.log('yesterday');
}, 1000);
}
}
Upvotes: 1