在设置间隔中设置变量不同的时间间隔

时间:2020-03-09 06:53:25

标签: javascript timeout setinterval

我有一个名为checkInactivity的函数,我想在可变的时间间隔内执行此函数。我正在获取时间间隔getInactiveTimePeriod,并且以可变的timeInMilliseconds来衡量。此变量(timeInMilliseconds)每次在其从数据库中选择的值时都具有不同的计时。我想用这个可变的时间间隔运行功能checkInactivity,即取决于timeInMilliseconds的值。 请帮我解决这个问题。预先谢谢你。

window.setInterval(function() {
    setInactiveMode();
    setInactiveTimePeriod();
    getInactiveTimePeriod();
 } , 100000);

 var applicationsopend = [];
 var applicationTabId = {};
 var timeInseconds;

 function getInactiveTimePeriod() {
    chrome.storage.local.get("user_inactive_time", function (obj) {
        timeInMinuts = obj.user_inactive_time;
        timeInMilliseconds = timeInMinuts *(60000);
        setTimeout(() => { 
            checkInactivity();
        }, timeInMilliseconds);

    });
 }

// getInactiveTimePeriod();

 function checkInactivity() {
    chrome.storage.local.get("user_inactive_mode", function (obj) {
        inactiveStatusMode = obj.user_inactive_mode;
        if(inactiveStatusMode == 'true') {
            var timeInseconds = timeInMilliseconds / 1000;
            chrome.idle.queryState(timeInseconds, function (state) {
                if (state === "active") {
                    chrome.storage.local.get( "appList", function (obj) {
                        appList = obj.appList.split(",");
                        var queryInfo = {
                            // active: false,
                        currentWindow: false
                        };
                        chrome.tabs.query(queryInfo, function (tabs) {
                            for (var i = 0; i < tabs.length; i++) {
                                var taburl = tabs[i]['url'];
                                currentApp = findApp(taburl);
                                currentHashUrl = CryptoJS.SHA1(currentApp).toString(CryptoJS.enc.Hex);
                                if($.inArray(currentHashUrl, appList) != -1 && currentApp != 'onionid' ) {
                                    var tabid = tabs[i]['id'];
                                    currentApp = findApp(taburl);
                                    applicationTabId[currentApp] = tabid;
                                }
                            }
                            //console.log(applicationTabId);
                            getapplicationlogouturl(applicationTabId);
                        });
                    }); 
                }
            });  
        }
    });
 }

1 个答案:

答案 0 :(得分:0)

获取间隔时间后,请使用setInterval()在给定的毫秒内无限期地调用该函数:

function getInactiveTimePeriod() {
    chrome.storage.local.get("user_inactive_time", function (obj) {
        let timeInMinutes = obj.user_inactive_time;
        let timeInMilliseconds = timeInMinutes * (60000);
        setInterval(() => {
            checkInactivity();
        }, timeInMilliseconds);
    });
}

请注意,如果要在其他地方使用此值,则应在更高的范围中声明它:

// global declaration
var timeInMilliseconds;

function getInactiveTimePeriod() {
    chrome.storage.local.get("user_inactive_time", function (obj) {
        let timeInMinutes = obj.user_inactive_time;
        timeInMilliseconds = timeInMinutes * (60000); // setting the globally declared variable
        setInterval(() => {
            checkInactivity();
        }, timeInMilliseconds);
    });
}

function checkInactivity() {
    var timeInseconds = timeInMilliseconds / 1000; // use the globally declared variable

    // ...
}