samy
samy

Reputation: 1969

javascript: setInterval execute with out stoping

I am creating a chrome extension and I need to use an interval.

The time the interval will go off every time is chosen by the user itself.

The function executes on every open tab and sends data to it.

My problem is that the "timer" dosn't work like it should, it just execute all at once like a normal while loop.

Here is the code:

chrome.tabs.query({}, function (tabs) {
//the times the code need to run is by the amount of tabs that is open
        setInterval(function () {
            if (i < tabs.length) {

                chrome.tabs.sendRequest(tabs[i].id, { input: "uCanSend",

                    userName: myName,
                    password: myPass,

                    subject: mySubject, msg: myMsg,

                    linkName1: myLinkName1,
                    linkURL1: myLinkURL1,

                    linkName2: myLinkName2,
                    linkURL2: myLinkURL2

                }, function (response) { });

            }
            i++;
        }, timeInMintus //the time that the code need to run every time);
    });

I have no idea what am I doing wrong. Any idea how to fix it that it will run correctly?

(sorry for my English)

Upvotes: 0

Views: 150

Answers (2)

epascarello
epascarello

Reputation: 207557

Chrome queues up setTimeout/setInterval calls on inactive tabs and changes the minimal timeout length.

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 360016

The second argument needs to be in milliseconds (thousandths of a second). Based on the variable name, it looks like you're passing minutes. For example, if you want the callback to run every minute:

setInterval(function () { /* snip */ }, 1000*60);

Upvotes: 2

Related Questions