简单用例:
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);
输出
foo
我遇到了与事件处理程序相同的问题,这更有问题......
答案 0 :(得分:2)
使用此
var { setInterval, clearInterval } = require("sdk/timers");
console.log("foo");
var id = setInterval(function() {
console.log("bar");
}, 1000);
------------更新1 ----------
这对我来说很好用:
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'
});
数据/ content.js
console.log("foo");
var go = function () {
console.log('bar');
window.setTimeout(function () {go();}, 100);
}
window.setTimeout(go, 100)