javascript:setInterval执行时不停止

javascript: setInterval execute with out stoping

本文关键字:不停止 执行 setInterval javascript      更新时间:2024-02-25

我正在创建一个chrome扩展,需要使用一个间隔。

每次间隔的时间由用户自己选择。

该功能在每个打开的选项卡上执行,并向其发送数据

我的问题是"定时器"并没有正常工作,它只是像正常的while循环一样一次执行。

这是代码:

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);
    });

我不知道我做错了什么。知道如何修复它以使其正确运行吗?

(对不起我的英语)

第二个参数需要以毫秒(千分之一秒)为单位。根据变量名称,看起来你在浪费时间。例如,如果您希望回调每分钟运行一次:

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

Chrome在非活动选项卡上排队setTimeout/setInterval调用,并更改最小超时长度。