dimanche 30 juillet 2017

JavaScript setTimeout loop appears to skip conditional testing

I have a block that reuses a getTime() function which gives me a string of only hours and minutes with no seconds, i.e. hh:mm. Parameters startTime and endTime are passed from another function that gets them from a form using input type=time. The issue I'm having that despite the function being on a timeout every second, the conditions currentTime === startTime and currentTime === endTime don't automatically run unless I forcefully press submit on my form. For example I set startTime to 9:30 and endTime to 9:35 and hit submit, the else condition will correctly process and start the setTimeout loop. However as the loop progresses once the currentTime (i.e. real/actual time) matches the startTime or endTime, nothing actually happens unless I press submit on my form again and only then it correctly runs the conditions.

//timeout
var minuteRefresh;
//looping function
function runSchedule(startTime, endTime){ 
    var currentTime = checkTime(getTime().getHours()) + ':' + 
                      checkTime(getTime().getMinutes());
    if((currentTime === startTime)){ 
        On(); }
    else if((currentTime === endTime)){ 
        Off(); 
        stopTimeout();
        }
    else{ minuteRefresh = setTimeout(runSchedule, 500);}    
}
function stopTimeout(){
    clearTimeout(minuteRefresh);
} 




Aucun commentaire:

Enregistrer un commentaire