3

I'm trying to use setInterval() within an extension developed with the firefox add-on sdk. Unfortunately, no matter how I call setInterval(), I keep getting the ReferenceError that setInterval is not defined.

I've simplified my code to illustrate the problem. The below code results in this error:

var globalTimer = 0;

function setGlobalTimer() {
    globalTimer = setInterval(testFunction, 15000);
    //globalTimer = setInterval(function() { testFunction(); }, 15000);
}

function testFunction() {
    console.log("testFunction just called");    
}

var { ToggleButton } = require("sdk/ui/button/toggle");
var button = ToggleButton({
    id: "my-button",
    label: "my button",
    icon: {
      "16": "./icon-16.png",
      "32": "./icon-32.png",
      "64": "./icon-64.png"      
    },
    onClick: handleClick
});


function handleClick(state) {
    testFunction();
    setGlobalTimer();
}

Both ways of setting the global variable globalTimer in setGlobalTimer(), one way is commented out, result in the following error when I click on my toggle button in Firefox:

ReferenceError: setInterval is not defined

I've scanned the stackoverflow site for questions about setInterval() not working, and in other cases, using the calls I am using has worked. But this code generates the above error for me.

If anyone can help me with this problem I would greatly appreciate it.

Thanks in advance.

erikvold
  • 15,988
  • 11
  • 54
  • 98
  • 3
    possible duplicate of [How can I add a recurring timer to a firefox addon?](http://stackoverflow.com/questions/5248035/how-can-i-add-a-recurring-timer-to-a-firefox-addon) – Alexander O'Mara Nov 02 '14 at 20:15
  • Awesome!! Solved with info from the thread Alexander referenced. – user4187425 Nov 03 '14 at 00:03
  • 1
    I used the `var tmr = require('sdk/timers');` solution, and then I set `globalTimer = tmr.setInterval(testFunction, 15000);` – user4187425 Nov 03 '14 at 00:39
  • 1
    `setInterval` is implicitly `window.setInterval`. You don't have access to the window object in SDK context. – willlma Nov 03 '14 at 04:14

1 Answers1

2

setInterval is not a global with the add-on sdk at the moment.

you need to require it, like so var { setInterval } = require("sdk/timers")

erikvold
  • 15,988
  • 11
  • 54
  • 98