Reputation: 8834
I am using the following jQuery script that countdowns the time until the $end
and when this happen it displays completed.
However when you change the time on your computer, the countdown is changed. What I had in mind is like those countdowns that Groupon has.
My question is how to change the time source in something that can not be changed, maybe the server's time? And when the timer reaches 0, how can I run a SQL query using PHP?
Am I totally in the wrong way?
Thank you.
<?php $end = 'september 1, 2011 19:24'; ?>
<script type="text/javascript">
$(document).ready(function() {
$("#time").countdown({
date: "<?=$end?>",
onComplete: function( event ){
$(this).html("<i>Completed</i>");
},
leadingZero: true
});
});
</script>
<p id="time" class="time"></p>
Upvotes: 0
Views: 3747
Reputation: 33153
It's ok to have a JavaScript countdown timer in most situations, but you can't rely on a client-side script not to "speed up" the timer. The user can always cheat the timer by changing the system clock, messing with the JavaScript variables manually, and so on.
Reading between the lines it seems like you want to do something like put a note in the database when a product comes available or an offer expires. If so, you should absolutely do this server-side, especially if the query must be run even if the user navigates away from the page and maybe returns only after the deadline. Maybe re-think the logic completely: instead of making a note in the database at the time the timer runs out, put the expiration time in the database beforehand that marks when the change comes into effect.
Another possibility is when the JavaScript timer hits the mark have the page make a call to the server. The server then checks if the call has been made at the correct time. If so, run the SQL query, but otherwise tell JavaScript that it should keep the timer running. (For example if the server thinks the call was made 10 minutes early, return data to JavaScript saying that it should reset the timer to 10 minutes and then try again.)
Upvotes: 0
Reputation: 41266
This may be the worst idea in existance, but perhaps you can simply calculate the time between now and the end date once, find the number of intervals you want, and then call setInterval
and manually decrement the value.
AKA:
var initialTime = new Date();
var endTime = /* Your Time when the thing is over */
var intervalId = setInterval(function()
{
// Every second, decrement the counter by 1 second.
// if counter is done, show end and then
clearInterval(intervalId);
}, 1000);
Upvotes: 1
Reputation: 1493
What you are asking is impossible. You can use setTimeout which will fire after a specific number of milliseconds.
Upvotes: 0