Lithy
Lithy

Reputation: 877

setInterval not called in tab content-script

Simple use case:

main.js

var tabs = require('sdk/tabs');

tabs.open({
    url: 'http://www.openstreetmap.org',
    onOpen: function (tab) {
        tab.attach({
            contentScriptFile: './content.js'
        });
    }
});

content.js

console.log("foo");
setInterval(function() {
    console.log("bar");
}, 1000);

output

foo

I get the same problem with event handlers, which is more problematic…

Upvotes: 2

Views: 266

Answers (1)

Alexey Sh.
Alexey Sh.

Reputation: 1937

Use this

var { setInterval, clearInterval } = require("sdk/timers");
console.log("foo");
var id = setInterval(function() {
  console.log("bar");
}, 1000);

------------ UPDATE 1 ----------

This works fine for me:

main.js

var tabs = require('sdk/tabs');
var self = require("sdk/self");

tabs.on("ready", function(tab) {

        console.log('opened', self.data.url('content.js'));
        tab.attach({
            contentScriptFile: self.data.url('content.js'),
            contentScriptWhen: 'ready'
        });
  })
tabs.open({
    url: 'http://google.com'
});

data/content.js

console.log("foo");

var go = function () {
        console.log('bar');
        window.setTimeout(function () {go();}, 100);
}

window.setTimeout(go, 100)

Docs

Another question

Upvotes: 2

Related Questions