0

I have a timestamp stored in a MySQL database, and I want to periodically (say every second), update the difference between the current time and the timestamp value stored on the database (of course without reloading the page) to show something like a 5 minute count-down timer. Right now. I'm thinking use setTimeout to periodically fire an AJAX request that gets this value. Is this the best approach? Instead, should I just run a client-side timer, and then sync it with the server-side timestamp every minute or so?

Note: This timer is responsible for an important function on my website, so it should be reasonably accurate.

Edit: I don't need a cron job, I just need a countdown timer that updates on a webpage like from 5:00 to 4:59 to 4:58... etc, automatically. If you're familiar with the one Megaupload had before it was shutdown, I need something similar.

hesson
  • 1,812
  • 4
  • 23
  • 35
  • 1
    `every second` = bad idea. A lot of network traffic, a lot of DB traffic, just to keep track of time? Computers already have something called a "clock" for that - I'm told they are very popular ;-) – DaveRandom Aug 10 '12 at 13:21
  • How would I do that? Would it be the clock from the client-side computer? I need to make sure that this timer is accurate and can't be tampered with by the client. – hesson Aug 10 '12 at 13:23
  • What is the use case here? Do you want to disable something or prevent the user from submitting a form after a certain period of time has elapsed, or something else? – DaveRandom Aug 10 '12 at 13:24
  • Basically I need a form to submit exactly 5 minutes after a user clicks the submit button. I need the user to see how much time is left before the form submits. Ideally, it should update every second so it looks like a real timer, kind of like the timer Megaupload had before. – hesson Aug 10 '12 at 13:27
  • 2
    OK well because of the nature of the way Javascript works, `exactly` is next to impossible to achieve (see [this](http://ejohn.org/blog/how-javascript-timers-work/) for more details). However, you can do something very close to it, and I'd suggest that what you want to do is do it all client-side (which is user-tamperable but minimal resource consumption) and more importantly use server side validation to prevent the user being able to tamper with it. So you set a value in the database at the beginning that says "don't accept the submit before x time" and then the client can handle the timer – DaveRandom Aug 10 '12 at 13:31
  • @DaveRandom - The Swiss love clocks. Hesson - A countdown timer is just a pleasantly feature of a web site, use JavaScript to do this. You cannot control what the user on the client side is doing. You are free not to accept the form. – Ed Heal Aug 10 '12 at 13:34
  • Forgot to mention the problems with network delay, client being heavily loaded ... – Ed Heal Aug 10 '12 at 13:36
  • Thanks, that'll work. Please post your comment as an answer so I can accept it and people with a similar problem can find it easily. – hesson Aug 10 '12 at 13:37

4 Answers4

1

It depends on what do you want to accomplish.

If you need to process something that is related to the website in general each 5 minutes for example then use a cron job.

If you want to improve user experiance by updating the comments list per example then use ajax.

thedethfox
  • 1,651
  • 2
  • 20
  • 38
1

So you want a server with all this stress.

One can assume that the clock on both server and client has a second that lasts as long on both and may take a week (if very bad) diverge.

But the client may think it is 1st Jan 1972 at midnight but your server thinks it is 14:25 on Fri 10th Aug 2012.

So just pass that date from the server to the client. Work out the diff. On the page you can put the servers date/time on the page just using Javascript.

As to the web site (server bit) just rely on the servers time. That you can control.

The countdown timer is just a pleasantry. But the server stuff is your asset and where you make your business. If you rely on the client some nasty person can just reset the time.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

Because of the nature of the way Javascript works, exact timers are next to impossible to achieve (see this for more details).

However, you can do something very close to it, and I'd suggest that what you want to do is do it all client-side (which is user-tamperable but minimal resource consumption) and more importantly use server side validation to prevent the user being able to tamper with it. So you set a value in the database at the beginning that says "don't accept the submit before x time" and then the client can handle the timer.

Here is a very small example of how it can be done

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
0

You can make a script that does that, and you can run it every second or so, depending on what OS your server runs on, with:

if linux { chronjob } http://www.thegeekstuff.com/2011/07/php-cron-job/

else if windows { windows scheduler } How do I run a PHP script using windows schedule task?

That's the safest and most efficient way to run client-side scripts.

Community
  • 1
  • 1
Andrei Cristian Prodan
  • 1,114
  • 4
  • 17
  • 34