set time interval as a specified value

42 views Asked by At

I want to set the time interval for the given function as a variable value as timeInMilliseconds.. but getting error as undefined..please help me to solve this. Thank you in advance .

window.setInterval(function(){
 chrome.storage.local.get("user_inactive_mode", function (obj) {
    inactiveStatusMode = obj.user_inactive_mode;
    if(inactiveStatusMode == 'true')   {
            chrome.storage.local.get("user_inactive_time", function (obj) {
            var timeInMinuts = obj.user_inactive_time;
            var timeInMilliseconds = timeInMinuts * 10;
            console.log(timeInMilliseconds);
            chrome.idle.queryState(timeInMilliseconds, function (state) {
                if (state != "active") {
                   ====
                   ====
                   =====
                }
            });
        });
    }
 });
} , timeInMilliseconds);
1

There are 1 answers

0
DNKO On BEST ANSWER

Initialize the variable timeInMillisecond outside the function. Reason why it says undefined, is because it's out of scope

     var timeInMilliseconds;
     window.setInterval(function(){
     chrome.storage.local.get("user_inactive_mode", function (obj) {
        inactiveStatusMode = obj.user_inactive_mode;
        if(inactiveStatusMode == 'true')   {
                chrome.storage.local.get("user_inactive_time", function (obj) {
                var timeInMinuts = obj.user_inactive_time;
                timeInMilliseconds = timeInMinuts * 10;
                console.log(timeInMilliseconds);
                chrome.idle.queryState(timeInMilliseconds, function (state) {
                    if (state != "active") {
                       ====
                       ====
                       =====
                    }
                });
            });
        }
     });
    } , timeInMilliseconds);

You can read here about the JS scoping: What is the scope of variables in JavaScript?