runEvery function is broken, it runs a script every x milliseconds if hour is after the given hour and minute is after the given minute, so given this input:
runEvery(()=>console.log('ran'),12,30,300000)
and the time now is 3:17 PM it will try running at 8:17 PM but now.getMinutes() >= mins will return false and the function will not execute, and every 5 hours it will always return false
But if you set minutes to 0 or someone starts the application after the provided minute it will execute every interval after the given time.
If the goal is to make a function that runs every day at a certain time, you need to get the time to the next occurrence and start the interval then, something like this:
functionrunEverydayAt(callback,hour,minute){// first we need to get the time until the next hour:minuteconstnow=newDate()// to check if the next time happens today check how many minutes// passed in the day and compare them with the inputconstnowInMinutes=(now.getHours()*60)+now.getMinutes()constinputTimeInMinutes=(hour*60)+minuteconstisNextTimeToday=nowInMinutes<inputTimeInMinutes// create a new date for the comparisonconstfirstTime=newDate()// and set its time to the input's timefirstTime.setHours(hour,minute,0)if(!isNextTimeToday){// if next time is today then add a dayfirstTime.setDate(now.getDate()+1)}consttimeUntilNextOccurrence=firstTime.getTime()-now.getTime()// finally set a timeout to start the first call// on time then every other call every 24 hourssetTimeout(()=>{// setInterval will start kicking in after 24 hours// therefore we need to invoke the first callback herecallback()setInterval(callback,1000*60*60*24)},timeUntilNextOccurrence)}// to check you can try invoking it with a time that's coming soonrunEverydayAt(()=>console.log('I ran!'),10,43)
Hey Youssif, I've just tried running the code in the console and it seems to be working.
You just need to set the time to be coming up in a minute or two so that you wouldn't wait for long for the next occurrence
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hello, nice list!
runEvery
function is broken, it runs a script every x milliseconds ifhour
is after the given hour andminute
is after the given minute, so given this input:and the time now is 3:17 PM it will try running at 8:17 PM but
now.getMinutes() >= mins
will return false and the function will not execute, and every 5 hours it will always return falseBut if you set minutes to 0 or someone starts the application after the provided minute it will execute every interval after the given time.
If the goal is to make a function that runs every day at a certain time, you need to get the time to the next occurrence and start the interval then, something like this:
Great!
Thanks for your feedback!
I'll check my code again and get back to you.
Hello!
I tried to run your function but it doesn't seem to be working.
Hey Youssif, I've just tried running the code in the console and it seems to be working.
You just need to set the time to be coming up in a minute or two so that you wouldn't wait for long for the next occurrence