2

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…

Lithy
  • 817
  • 12
  • 23

1 Answers1

2

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

Community
  • 1
  • 1
Alexey Sh.
  • 1,734
  • 2
  • 22
  • 34
  • My `setTimout` issue is in the content script, which cannot access the require API. https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts – Lithy Mar 12 '15 at 13:20
  • Ok so actually the only important difference is that you attach the content script when tab is `ready` instead of when tab is `open`. Thx – Lithy Mar 12 '15 at 14:33