Getting ReferenceError: setInterval is undefined

I am trying to use setInterval () in an extension developed with add-on sdk firefox. Unfortunately, no matter how I call setInterval (), I keep getting a ReferenceError that setInterval is undefined.

I have simplified my code to illustrate the problem. The code below results in an 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 of the ways commented out, results in the following error when I click the toggle button in Firefox:

ReferenceError: setInterval is undefined

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

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

Thanks in advance.

+3


source to share


1 answer


setInterval

is not global with sdk added at the moment.



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

+2


source







All Articles