0

I want to run a function at specific time points in a particular timeframe, say from 5:30 to 14:30. The specific time points would be increments of 3 minutes from 5:30 like so, 5:30, 5:33, 5:36,...,6:00 etc until 14:30. I thought the logic would be as simple as checking if the current time in integer format was a multiple of 3 and checking if the "seconds" in the current time was "00", since I want to run it only at the beginning of these specific time points. But this logic is incorrect. Since a lot of time points such as 10:00, 11:00, 13:00, 14:00 etc are not divisible by 3. Also, the browser freezes because of the while loop. How do I go about this and what is the correct logic here?

myFunc = () => {
    //Some Task
};
myFunc();

var dateToday = Date();

while (Number(dateToday.getHours()+(dateToday.getMinutes()<10?'0':'') + dateToday.getMinutes())) >= 530 && Number(dateToday.getHours()+(dateToday.getMinutes()<10?'0':'') + dateToday.getMinutes()) <= 1430){ 
    var dateTodayCheck = new Date(),
    hoursCheck = dateTodayCheck.getHours(),
    minutesCheck = (dateTodayCheck.getMinutes()<10?'0':'') 
   + dateTodayCheck.getMinutes(),
   secondsCheck = (dateTodayCheck.getSeconds()<10?'0':'') 
   + dateTodayCheck.getSeconds();
   var timeNowCheck = Number(hoursCheck+minutesCheck)
   var remainder = timeNowCheck % 3
    
   if (remainder === 0 && secondsCheck==="00") {
       myFunc();
   };
};
Cosmic
  • 25
  • 6
  • Does this answer your question? [Call a javascript function at a specific time of day](https://stackoverflow.com/questions/4455282/call-a-javascript-function-at-a-specific-time-of-day) – derpirscher Jun 19 '22 at 11:39
  • No, that doesn't. For instance, let's say, I have the app open at 5:29 and then a minute later at 5:30, I want the app automatically calling that function. In the example you shared, I would have to refresh the page at 5:30 for the app to realize that the time is 5:30 and then it would call that function. I want the app to be constantly monitoring, whether the time is, 5:30, 5:33, 5:36 etc....all the way upto 14:30. – Cosmic Jun 19 '22 at 12:24

1 Answers1

0

Okay, I got it to work for me. I came up with new logic. I hard-coded the minutes, I wanted the function to run at, ex: [0, 3, 6, 9, 12, 15,....,60]. Now say the current time is 6:35:15 the "minutes" part of the current time would be 35, so I checked for the number from my array that was closest to 35, which would be 36 (Found the code for this online). Then I took the difference between the closest value and the "minutes" part of the current time, in this case, 36 - 35 = 1 (1 minute). Later I accounted for the seconds, so 1 min - 15 seconds = 45 seconds. Finally, passed 45 seconds into a Timeout Function which contains an Interval Function that calls myFunc() every 3 minutes. This way if I open the app at 6:35:15, the program would know to wait for 45 seconds and call myFunc() at 6:36, and every 3 minutes from that point.

var dateToday = new Date(),
hours = dateToday.getHours(),
minutes = (dateToday.getMinutes()<10?'0':'') + dateToday.getMinutes(),
seconds = (dateToday.getSeconds()<10?'0':'') + dateToday.getSeconds();
var timeNow = hours+minutes

if(Number(timeNow) >= 530 && Number(timeNow) <= 1430){ 
    const closest = [0, 3, 6, 9, 12, 15, 18, 21, 24, 
    27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 
    60].reduce((a, b) => {
        let aDiff = Math.abs(a - Number(minutes));
        let bDiff = Math.abs(b - Number(minutes));

        if (aDiff === bDiff) {
            return a > b ? a : b;
        } else {
            return bDiff < aDiff ? b : a;
        };
    });

    var difference = closest - Number(minutes)
    var timeoutTime = difference === -1 ? 2 : difference
    var timeoutTimeCorrected = (timeoutTime * 1000 * 60) - (Number(seconds) * 1000) 
    setTimeout(() => {
        myFunc();
        setInterval(() => {
            myFunc();
        }, 180000);
    },timeoutTimeCorrected);
};

Please let me know, if there is a more elegant way to do this.

Cosmic
  • 25
  • 6