Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting ReferenceError: setInterval is not defined

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.

like image 944
user4187425 Avatar asked Dec 08 '25 21:12

user4187425


1 Answers

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")

like image 151
erikvold Avatar answered Dec 11 '25 11:12

erikvold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!