SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot Mahz's Avatar
    Join Date
    Mar 2006
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Keeping a displayed timer updated realtime

    I'm sort of making a game from the ground up to learn my way around ajax and its interaction with php and myself. basically, I want to have a master timer on the server (independent of server time) and have certain things happen for the user depending on what time it is. I want to display the master time on a page and have it update realtime by the second but I can not figure out how to do this.

    To start off with something basic, how would I go about keeping a realtime updating timer that's being run on the server to display on a page and update per second? What's the best way of keeping a sort of master counter stored on a server? I'm using PHP and MYSQL.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    paste this into a php file and upload it to your server.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Untitled Document</title>
            <link rel="stylesheet" type="text/css" media="screen" />
            <style type="text/css"></style>
            <script type="text/javascript">
                var time;
                function setup() {
                    time = new Date(<?php echo time(); ?> * 1000);
                    //alert(time);
                    setTimeout('updateTime()', 1000);
                }
                function updateTime() {
                    setTimeout('updateTime()', 1000);
                    time.setSeconds(time.getSeconds()+1);
                    document.getElementById('test').value = time;
                }
                window.onload = setup;
            </script>
        </head>
        <body>
            
            <input type="text" id="test" style="width: 300px;" />
    
        </body>
    </html>
    When the page is loaded the server time is writen into a javascript date, and that is then updated every second.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •