hi,

I have been searching for a php or a javascript that is a timer/stopwatch. It only needs to show minutes from 0 to 90.
But the thing is, I need a script that can do the following:

  • I can pause the timer, so when I resume it - it continues where it left of. I need also the option to reset it.
  • And most important, the timer MUST NOT reset when/if the page is refreshed.

Can anyone help me with this? I have searched this forum and many other script sites, but no luck 🙁
(The script is for live soccer matches, it is supposed to indicate minutes durring a match).

Thanks is advance.

    I would insert the starting time into a DB, and every time the page is loaded, calculate the number of seconds/minutes that have passed from the time in the database to the time the script is loaded.

      hmm, good idea! but I can't see how it is possible to pause the values in this function....? It dosn't mater if I pause the visual time, the real ime will continue.

        So add a 'duration' column to the start time, and every time it is 'paused', calculate the duration up to now, and then clear the 'start time' field. When you resume, insert the current time into the 'start time' field. The total time would be time from 'start time' to now, plus the number of seconds in the 'duratation' field.

        Get what I mean?

          Alternatively if you wanted to use Javascript, you could have a function that would call itself recursively using setTimeout() to call itself every minute. The function would add 1 to the elapsed time, and store (or update) the value in a cookie.

          To implement pausing, you'd have a variable like isPaused, and the above function would only run if isPaused was false. The pause function would set isPaused to true, and the unpause function would set it to false and call the timer function.

            The only problem with Javascript would be that the current duration would be lost if the user refreshed or closed the window, which is exactly what Manijak was trying to prevent.

            Try implementing my suggestion using MySQL and let me know how far you get. If you get stuck, post what you have, and I'll tweak it to show you what I meant.

              Originally posted by bradgrafelman
              The only problem with Javascript would be that the current duration would be lost if the user refreshed or closed the window, which is exactly what Manijak was trying to prevent.

              That's what the cookie's for 🙂

                Sounds resource consuming. Also sounds like you're depending on the client's browser to support and allow Javascripting.

                I'm not trying to be cocky or shoot down your suggestion, I'm merely pointing out that my solution centered on using PHP and a database simply because there is little chance the user's environment (browser, operating system, etc.) can interfere with the operation of the "stopwatch."

                If you ignore the bit about unsupported clients or security settings prohibiting Javascript, then Javascript would probably work. Let's see what Manijak decides to try.

                  thanks for helping me out guys.
                  yes, the javascript method that linus showed, would work. Altho I am trying to make it simple.

                  I tryed to understand your method bradgrafelman, but I can't seem to get it fully.
                  I have two field in the DB: start_time and duration. and the script needs to calculate the time between the start time and the durration??? How?

                  I tryed something like this with for loops, but the entire site first loaded the for loop, then the rest of the site, which took forever.

                  (I am very new to php, I only know the basics 😛)

                    I think Bradgrafelman's implementation of pausing works like this:

                    When the timer first starts, you insert the current time into the start_time field.

                    Whenever it's paused, you calculate the time so far (current time minus the start_time field), add that to the value of the duration field, and put the result into duration. The first time it's paused, duration will be 0, but as it's paused again and again the duration will update.

                    When it's unpaused, insert the current time into start_time.

                    When it's finished, do the same thing, update the duration field to (current_time - start_time + duration), and you have the total time.

                      Sorry - been swamped with work.

                      Yes, linus, that's exactly what I meant, kudos to you!

                      If I get some free time, I can write up some code, or you can go ahead and try it on your own.

                        Write a Reply...