timestamp grab interval with a javascript timer?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
logic-8
Forum Newbie
Posts: 5
Joined: Sun Jan 22, 2006 6:29 pm

timestamp grab interval with a javascript timer?

Post by logic-8 »

I’m working on a multi-player game site, and I’m having problems with my lobby/rooms – The set-up is : The ‘lobby’ shows the available rooms, how many games are open in that room, number of players in that room, etc…
When a player joins a room, it updates the lobby timestamp.
The problem is, I’m trying to use javascript to time the timestamp grab… and nothing’s happening… Here’s the basic code: this is just for the test. I'm pretty new to PHP, but I think this must be a javascript error?

Code: Select all

<HTML>
<HEAD>
<TITLE>timer test</TITLE>
<SCRIPT LANGUAGE="Javascript">
<!--

var x = 3
var y = 1

function startClock() {
  x = x-y 
  document.frm.clock.value = x

  if(x==0){

     <?PHP
      // get lobby timestamp

      include("scripts/dbcon.php");
      include("scripts/db.php"); 

      $query = "SELECT * FROM rooms_ts";
      $result = mysql_query($query);

      $tsNum = mysql_num_rows($result);

      if (!$tsNum) {
           echo "ERROR>> TIMESTAMP INVALID";
      } else {
           $newTS = mysql_fetch_row($result);
      }
      }
     ?>

  var stamp  = <?= $newTS; ?>
  document.frm.ts.value = stamp

  x=3;
}

setTimeout("startClock()", 1000)
} 
//-->
</SCRIPT>

</HEAD> 
<BODY BGCOLOR="#FFFFFF" onLoad="startClock()"> 
 countdown in first box, timestamp in second
 <FORM NAME="frm"> 
   <INPUT TYPE="TEXT" NAME="clock" SIZE=4 > 
   <INPUT TYPE="TEXT" NAME="ts" SIZE=5 > 
 </FORM>
</BODY>
</HTML>
Any help would be fantastic, this problem has plagued me now for a while! argh. -logic
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the php code you've placed will only be run when the page itself is executed. So the database will only be queried one time per page request..
logic-8
Forum Newbie
Posts: 5
Joined: Sun Jan 22, 2006 6:29 pm

Post by logic-8 »

Thanks -- does anyone have any idea how to fix this? Basically I need it to check the timestamp once/second, javascript or not, I don't care, but I'd prefer it didn't physically re-load the page every second to do so. Then of course if the timestamp differs from the one saved in the session var the page should update -- I've tried using sleep(1) and I can update the timestamp fine, but I cant seem to get the page to show, much less update. Like I said I'm fairly new to PHP. I wrote the multi-player game in shockwave, so if all else fails I can just build the lobby in there, but it causes the computer to 'crunch' every time it grabs the timestamp (it sounds like a windows update check or a download), and I want to try to avoid freaking people out, as there seems to be some skepticism about shockwave as it is. Thanks again, Logic.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you may want to use a pulse (with margin of error forgiveness) through Ajax. You can tell Ajax to query in the interval rate you've specified. This would also help (although cheatable) that a user is connected. If someone misses the pulse for say 30 pulses, they're logged off?
logic-8
Forum Newbie
Posts: 5
Joined: Sun Jan 22, 2006 6:29 pm

Post by logic-8 »

cool, I'll check that out! I know nothing of Ajax but there's no time like the present. Thanks again. -Logic
Post Reply