Jump to content

do something every X seconds


TeddyKiller

Recommended Posts

The Javascript.

 

function updateTable() {
  $.load('update.php');
}
setInterval( "updateTable()", 10000 );

 

You'll also need jquery which you get by adding the following line to the <head> of your document.

 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"><?script>

 

Then just do whatever needs doing within update.php

It doesn't work, what I'm assuming, when the echo comes it's not starting the function.

Can you help?

 

<?php
include("lib.php");
define("PAGENAME", "Resting");
$player = check_user($secret_key, $db);
?>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
function updateTable() {
  $.load('enhp.php');
  alert("You healed 1HP and 1Energy point!");
}
</script>
</head>
<?php
echo '<center>';
if ($player->hp == $player->maxhp && $player->energy == $player->maxenergy)
{
echo "<i>You have full HP and Energy.</i>\n";
exit;
}
else
{
if ($_GET['act'] == "rest") {
	echo '<script type="text/javascript">setInterval("updateTable()",12000);</script>';
	echo 'You are now resting. You will gain 1hp and 1energy point every 5 seconds. To unrest type "unrest"';
    }
}
echo '</center>';
?>

<html>
<?php
include("lib.php");
define("PAGENAME", "Resting");
$player = check_user($secret_key, $db);
?>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
function updateTable() {
  $("#resting").load('enhp.php');
  alert("You healed 1HP and 1Energy point!");
}
</script>
</head>
<body>


<?php
echo '<center>';
if ($player->hp == $player->maxhp && $player->energy == $player->maxenergy)
{
   echo "<i>You have full HP and Energy.</i>\n";
   exit;
}
else
{
   if ($_GET['act'] == "rest") {
      echo '<div id="resting"></div>';
      echo '<script type="text/javascript">setInterval("updateTable()",12000);</script>';
      echo 'You are now resting. You will gain 1hp and 1energy point every 5 seconds. To unrest type "unrest"';
    }
}
echo '</center>';
?>
</body>
</html>

 

That should work. Your problem was that the .load jQuery function requires a selector to load into. I've created the div resting. If enhp.php outputs any text it will be loaded into #resting. But as enhp is just getting database querys (isnt it?) it shouldnt do anything. But that should work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.