[NEW] Run function once hour after specific time

DaAwesomeP

I know I made a previous post, on this, but I hasn't been working correctly.

I want to run a function ([FONT=courier new]weatherForecast_Update[/FONT]) one hour after the given time ([FONT=courier new]forecast_loaded[/FONT]). I am getting the correct time in [FONT=courier new]weatherForecast_Update_time[/FONT] in milliseconds*, but the timeout runs continuously. MomentJS is working perfectly, and if the corrected code could use it, that would be great.

I commented out the [FONT=courier new]setTimeout [/FONT]because it will run too often and freeze up your browser (when testing it). It gives a "stack size exceeded" error.

Thanks for any help! (great forum )

*Note: You can easily convert time and other units with Google by typing Xms in min where X is the time, ms is the current unit (in this case milliseconds) and min is the target unit (in this case minutes). The answer will even pop up in the suggestions!

root

To run a function onload and then once per hour thereafter, you need to first off have a starting reference... Then set an interval timer

The interval timer will call the function every hour until the browser is closed.

DaAwesomeP

I guess I didn't explain this very well. I want to run the function one hour after the given time. I have a library that gets and caches NOAA weather Forecasts once per hour (suggested by NOAA). The function this triggers loads the new forecasts with AJAX. Instead of updating it one hour after page load, I would prefer to update it one hour after the forecast was fetched. That time is [FONT=courier new]forecast_loaded[/FONT][FONT=arial]. [/FONT]The code above is just an example of the timestamps it outputs. I would prefer to use MomentJS (it processes it fine and keeps it neat), but I can alter the PHP [FONT=courier new]Date()[/FONT] if needed.

root

When the page loads, the function will get called one hour after.

If you are setting a time when the page is called, then its simply a case of adding one hour of milliseconds to a figure from a time function like so.

I figured you were having the time reference added in and thought that you could have worked out that all you needed to do was add milliseconds to a time reference that can be used to return milliseconds. Simple math really.

DaAwesomeP

Err... OK, so [FONT=courier new]forecast_loaded[/FONT] is a completely different time than the page load. The page load is [FONT=courier new]moment_now[/FONT]. If I access the page a 5:30 PM, and the forecast was last updated at 5:00 PM, then it should update in 30 minutes, as that is one hour since 5:00 PM (it would update at 6:00 PM). The [FONT=courier new]forecast_loaded[/FONT] is converted to milliseconds since the Unix Epoch, which is why I am have [FONT=courier new]moment_now[/FONT][FONT=courier new] [FONT=arial]subtracted from [/FONT]moment_forecastloaded_add1h[FONT=arial] (I am adding one hour with MomentJs[/FONT])[/FONT][FONT=arial].[/FONT] So, I am adding 1hr to the forecast load time. I am then subtracting the current time from it to get the amount of time until it needs to reload again. I then check to make sure it is positive so that I don't have infinite loads.

root

DaAwesomeP;1297097 wrote:

Err... OK, so [FONT=courier new]forecast_loaded[/FONT] is a completely different time than the page load. The page load is [FONT=courier new]moment_now[/FONT]. If I access the page a 5:30 PM, and the forecast was last updated at 5:00 PM, then it should update in 30 minutes, as that is one hour since 5:00 PM (it would update at 6:00 PM). The [FONT=courier new]forecast_loaded[/FONT] is converted to milliseconds since the Unix Epoch, which is why I am have [FONT=courier new]moment_now[/FONT][FONT=courier new] [FONT=arial]subtracted from [/FONT]moment_forecastloaded_add1h[FONT=arial] (I am adding one hour with MomentJs[/FONT])[/FONT][FONT=arial].[/FONT] So, I am adding 1hr to the forecast load time. I am then subtracting the current time from it to get the amount of time until it needs to reload again. I then check to make sure it is positive so that I don't have infinite loads.

Sorry I've been vague.

Which is why you get the date and time from PHP that has been provided with the time you want.

These things are fairly simple to do and shouldn't have to keep on sign posting the way for you.

In a nutshell...

your time you want for your forecast, its its 5.30 in the morning then make that string 5.30 in the morning.

this is written in to the previously illustrated in the code I showed

Bobs your Uncle... (and your father too.)

Simples.

root

Ok, holding your hand here, this function I wrote, and tested as a POC will run in background of your site code, it is an adaptation of a clock I made for timing purposes.

In short, where it says / MAKE STUFF HAPPEN / is the area you put your function that you want to run, the clock as I said runs and counts down to 0, after 0 the hour of milliseconds gets added and it begins countdown to next whatever you want.

Runs hourly from whenever the $lastReport is set to, you will have to research your timezone setting on the PHP side of things.