How to implement a timer with ajax. A real-time solution??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fjm
    Contributor
    • May 2007
    • 348

    #1

    How to implement a timer with ajax. A real-time solution??

    Hi everyone, I usually hang out on the php side because its what I know, but I am faced with a problem that I honestly have no idea how to solve and figured I would try my luck over here. I am hoping that a few of you AJAX gurus can point me in the direction I need to go. Any help or comments would be appriciated.

    I have an application written in php and I need to use some kind of control that will keep a timer going. This is a dispatching type of program that uses a database. When the user enters data into the db, this "timer" should start counting and when it reaches a time specified by the system administrator, the app should play a sound byte and flash that particular row of data indicating to the user that this time limit is up.

    Could someone please help me with a solution? I was told that AJAX could do this. If not, would anyone know what method may work for this problem?

    Thanks,

    Frank
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    When you say the user "enters data into the db", do you mean via a PHP script? You may not need Ajax at all, though you would need JavaScript. What you need to use is setTimeout. You could make a request using Ajax. When the request is complete, start the timer which calls a function on completion. This function would play the sound file and flash the row or whatever.

    Comment

    • fjm
      Contributor
      • May 2007
      • 348

      #3
      Thanks for the response acoder. Yes, the user will enter data into the database via a php script. The timout should be row specific. In other words, if there are 100 rows of data (Very very unlikley) each row will have a different timout.

      Looking at the DOM code:

      Code:
      [i]timeoutID[/i] = window.setTimeout([i]func[/i], [i]delay[/i], [[i]param1[/i], [i]param2[/i], ...]);
      Wouldn't I need something to tell setTimout when the row was created or modified? Also, I don't know anything about js, so forgive me if I sound square but isn't window.setTimou t for the window? I would need this function for rows within the window. :) Again, please forgive my nubish glow.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You mentioned in the first post that the timer would start when the user has entered data into the database. If you're using Ajax, you can start the timer when the request is complete, or if you want, when you start the request (though that may be unreliable). setTimeout can call a function which can deal with the required functionality for each row.

        In case you need something to get you started with Ajax, here's a quick howto. If you need help implementing the Ajax code, let me know.

        Comment

        • fjm
          Contributor
          • May 2007
          • 348

          #5
          Hey acoder. Thanks for the link. Unless I am missing something, (and I probably am) the example ajax in the link you gave does nothing more than populate a dropdown box from a db. I can do that with straight php.

          What am I missing :)

          Frank

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Yes, that's correct. It was, after all, only an example. The difference would be that it would populate the drop down without leaving the page which PHP alone couldn't do. Alternatively, you could use a framework/library to do it for you. Many have animation effects too.

            Comment

            Working...