I would like to make an hourly offer. So every hour a new offer should be shown.
I’ve a PHP file that will show the offer like offer.php?hour=22
This will show the offer from 10pm.
How can I create a check every minute in jQuery that will execute an ajax load of my php file if the hour is 22. If not, nothing should happen to reduce the amount of refreshes/connections.
I’m not experienced in jQuery, so sorry for asking.
2
Answers
I would advise that you you use PHP to determine the current Hour of the Day. This way external Users can’t force an alternate Hour.
For example if it is 2:00 PM (hour 14), and the user sends a Request to
offer.php?hour=15
the system might provide a offer that was not intended for this time.Also, each browser can be using it’s own Timezone Offset. Do you want the Hour your User is in or the Hour that your server has?
Consider this:
When
offer.php
is loaded, the Hour of the Day will be populated into$hour
.You can then use
setinterval()
in JavaScript to refresh an image or offer on your page and never worry that the User will bypass this.In your page, you can just have a Countdown timer to the next Hour.
See More:
Refreshing content with jQuery
Sending an ajax request
Doing it hourly
One last problem
If you need to change the offer on exact hours, then you will need to wrap a
setTimeout
around yoursetInterval
and you will also need to display the first change in a special way.