Search code examples
javascriptfor-loopsettimeout

mutliple set timeouts running at once


so, i have a for loop that I want to run multiple set timeouts at once so it can continually check if it is a certain time for multiple times. i can't just check for all multiple times all at once, the check needs to repeat after different amounts of time. it only continues to repeat the last iteration of the loop, the last dict in lat.

my approach simplified a bit:

lat = list of dictionaries with some values that differentiate them + times
for(i = 0; i< lat.length; i++){

if(lat[i][differentiate value]) = {

function checktime(){
   if(currenttime != lat[i]){
setTimeout(checktime,30000)
}
else{
console.log("yay the time is right!")
}

}

else if(lat[i][differentiate value]) = {

function checktime(){
   if(currenttime != lat[i]){
setTimeout(checktime,50000)
}
else{
console.log("yay the time is right!")
}

}

}

How would I go about making this (its for a notification app)

original code: (each value in later looks like [[75,null,[7,28,2021]], null,[[9,52,"p"]],"e&r"] with the amount of notifications, a preset date, the date for these reminders to be on, by time, which would be the first if, and the array of at times. for testing I had 2 laters with 2 different at times):

 chrome.storage.sync.get('later', function(data){
        if (data.later !== null){
        var lat = data.later;
        
        for(i = 0; i< lat.length; i++){
            var currentDict = lat[i];
            
                if(currentDict['notis'][1] !== null){
                    console.log("cheese")
                    var by = currentDict['notis'][1];
                    console.log(by)

                    const d = new Date();
                        var hr = d.getHours();
                        var min = d.getMinutes();
                        var sec = d.getSeconds();
                        
                        var da = currentDict['notis'][0][2][1];
                        var mo = currentDict['notis'][0][2][0];
                        var ye = currentDict['notis'][0][2][2]
                        var h = by[0];
                        var m = by[1];
                        var ampm = by[2];
                        if(ampm == "p"){
                        h = h + 12;
                        }

                        var byMS = h*3600000 + m*60000;
                        var currentMS = hr*3600000 + min*60000 + sec*1000;

                        //check if right date then check if time is lesss than

                    
                    function checkdate(){
                        var day = d.getDate();
                        var month = d.getMonth() + 1;
                        var year = d.getFullYear();
                        if(da == day && mo == month && ye == year){
                            var amt = 0;
                            function checktime(){
                            if(byMS >= currentMS){
                                //noti and delete
                                var int = setInterval(function(){

                                    chrome.notifications.create({
                                        title: currentDict['name'],
                                        message: "do da " + currentDict['name'],
                                        iconUrl: "logo.png",
                                        type: "basic"
                                
                                    })
    
                                    amt++
                                    console.log(amt)
                                    const dddd = new Date();
                                    console.log(dddd.getMinutes() + " / " + dddd.getSeconds())
                                    if(amt >= currentDict['notis'][0][0]){
                                        clearInterval(int)
                                        console.log("done")
                                        //ju finish taht
                                        console.log(lat)
                                            lat.splice(lat.indexOf(currentDict),1)
                                            console.log(lat)
                                            chrome.storage.sync.set({'later': lat})
                                        
                                    }
                                }, (byMS-currentMS)/currentDict['notis'][0][0])
                            }
                            else{
                                setTimeout(checktime,30000)
                            }
                        }
                        checktime();
                        }
                        else{
                            setTimeout(checkdate,66400000)
                        }
                    }
                    checkdate();
                }
                else if(currentDict['notis'][2] !== null){
                    

                    console.log("cheese")
                    var at = currentDict['notis'][2];
                    console.log(at)
                    
                    var arrayat = [];
                for(j = 0; j<= at.length-1; j++){
                    var atcycle = at[j];
                    console.log(atcycle)
                    const ddd = new Date();
                    var hr = ddd.getHours();
                    var min = ddd.getMinutes();
                    
                    var da = currentDict['notis'][0][2][1];
                    var mo = currentDict['notis'][0][2][0];
                    var ye = currentDict['notis'][0][2][2]
                    var hrat = atcycle[0];
                    var minat = atcycle[1];
                    var ampm = atcycle[2];
                    if(ampm == "p"){
                    hrat = hrat + 12;
                    }
                    console.log(hrat + "/" + minat + "/" + ampm)
                    if(hr <= hrat){
                           
                        var temparray = [];
                        temparray.push(hrat)
                        temparray.push(minat)
                        arrayat.push(temparray);
                        console.log(arrayat)
                         }
                          
                    else if(hr == hrat){
                            if(min<minat){
                                var temparray = [];
                                temparray.push(hrat)
                                temparray.push(minat)
                                arrayat.push(temparray);
                                console.log(arrayat)}
                                }       
                        }      
                        console.log(arrayat.length)     
                    function checkdate(){
                        console.log(arrayat.length)
                        console.log("date")
                        const d = new Date();
                        var day = d.getDate();
                        var month = d.getMonth() + 1;
                        var year = d.getFullYear();
                        if(da == day && mo == month && ye == year){
                            function check(){
                                console.log(arrayat.length)
                                console.log("check")
                                for(l=0; l<arrayat.length; l++){
                                    console.log(arrayat.length)
                                const dd = new Date();
                                var hr = dd.getHours();
                                var min = dd.getMinutes();
                                console.log(arrayat[l][1])
                                console.log(min)
                                if(arrayat[l][0] == hr && arrayat[l][1] == min ){ //at one of the times
                                    console.log(arrayat)
                                    arrayat.splice(l,1)
                                    console.log(arrayat)
                                    if(arrayat.length == 0){
                                        lat.splice(lat.indexOf(currentDict),1)
                                        chrome.storage.sync.set({'later': lat})
                                        console.log(lat)
                
                                    }
                                    chrome.notifications.create({
                                        title: currentDict['name'],
                                        message: "do da " + currentDict['name'],
                                        iconUrl: "logo.png",
                                        type: "basic"
                                
                                    })
                                    //add noti with name and delete it
                                    console.log(arrayat)
                                    check();
                                     }
                                }
                                console.log(arrayat.length)
                                if(arrayat.length !== 0){
                                    console.log("and repeat")
                                    setTimeout(check,15000);//SETINTERVAL INSTEAD? ANDCLEAR
                                }
                                
                            }
                            check();
                            }
                            else{
                            setTimeout(checkdate,66400000)
                            }
                     }
                     checkdate();
                             
                }

                
            }
           }
          
    })
    }





Solution

  • This is the wrong approach. You know the times, so you know how far off they are in time. You don't need to check over and over again if the time has arrived. Instead work out how many milliseconds until the time in question, then set a timeout for that time.