一次一页上有多个计时器

时间:2016-09-02 15:17:21

标签: javascript timer

好的,我想知道如何在一页上获得两个倒数计时器?

    <script type = 'text/javascript'>
   window.onload=function() {
   setInterval("<?php
$array = count($settime);
for ($i = 0; $i < $array; $i++) {
    $thitime = explode(":", $settime[$i]);
    echo "timeleft(" . $thitime[1] . ", " . $thitime[0] . "); ";
}
?>", 1000);
   }
   function timeleft(string, id)
   {
      var xmlhttp=GetXmlHttpObject();
      if(xmlhttp==null) { alert("Sorry, Your browser doesnt support HTTP Requests");
      return;
      }
      var elem = "time" + id;
      var load = "time_left.php?string=" + string;
      xmlhttp.onreadystatechange=function() {
         if(xmlhttp.readyState==4) {
            document.getElementById(elem).innerHTML = xmlhttp.responseText;
         }
      }
      xmlhttp.open("GET", load, true); 
      xmlhttp.send(null);
   }
   function GetXmlHttpObject() {
   var xmlhttp=null;
   try {
        xmlhttp=new XMLHttpRequest();
       }
        catch (e) {
                   try {
                        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
                       }
                        catch (e) {
                                   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                                  }
                   }
   return xmlhttp;
   }
   </script>

我尝试关闭两个计时器的页面只加载其中一个计时器,我尝试了多种尝试来修复问题,并且似乎无法弄清楚它是什么错误用它.. 我甚至试过制作两个不同的计时器脚本,但是没有用。

以下是我要在两个计时器上运行的页面的完整代码..

    <?php
/*------------includes--------------*/
include('./includes/connections.php');
include('./includes/brain_file.php');
include('./includes/style_top.php');
/*------------includes--------------*/
if ($pl['my_tuts_on'] == 'yes') {

    echo "<tr bgcolor=#E6E6E6><td>
             Here you can view all the inmates currently in jail. You may attempt to bust/bail them, Bailing them costs a fee, Busting them uses 10 energy and is not a garantee you will bust them, Just be carefull not to get caught and end up in jail yourself. 
             </td></td></tr></table></center><hr/ width=95%>";
}

echo "<center><main>Jail</main><hr width='95%'/>";



if ($pl['my_hosp'] > gmtime()) {
    echo "Sorry this page is not viewable while in hospital!<hr width='85%'/>";
    include('./includes/style_bottom.php');
    exit();
}
$_GET['page'] = abs(intval($_GET['page']));
$min          = ($_GET['page'] > '1') ? (($_GET['page'] - 1) * 25) : $min = 0;
$q_ry         = array();
$q_ry         = "SELECT `playerid` FROM `members`
            WHERE `my_jail` > '" . mysql_real_escape_string(gmtime()) . "'";
$tot          = array();
$tot          = mysql_query($q_ry);

if ($joh['my_jail'] > gmtime()) {
    $settime[] = $pl['playerid'] . ":" . $pl['my_jail'];
    echo "<br><font size=2><b>", stripslashes($pl['jail_reason']), "</b>
          <br>You will be in jail for another <span id = 'time" . $pl['playerid'] . "'><b>" . gettimeleft($pl['my_jail']) . "</b></span> yet!</font><br><br><hr width='85%'>";
}
?>
   <script type = 'text/javascript'>
   window.onload=function() {
   setInterval("<?php
$array = count($settime);
for ($i = 0; $i < $array; $i++) {
    $thitime = explode(":", $settime[$i]);
    echo "timeleft(" . $thitime[1] . ", " . $thitime[0] . "); ";
}
?>", 1000);
   }
   function timeleft(string, id)
   {
      var xmlhttp=GetXmlHttpObject();
      if(xmlhttp==null) { alert("Sorry, Your browser doesnt support HTTP Requests");
      return;
      }
      var elem = "time" + id;
      var load = "time_left.php?string=" + string;
      xmlhttp.onreadystatechange=function() {
         if(xmlhttp.readyState==4) {
            document.getElementById(elem).innerHTML = xmlhttp.responseText;
         }
      }
      xmlhttp.open("GET", load, true); 
      xmlhttp.send(null);
   }
   function GetXmlHttpObject() {
   var xmlhttp=null;
   try {
        xmlhttp=new XMLHttpRequest();
       }
        catch (e) {
                   try {
                        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
                       }
                        catch (e) {
                                   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                                  }
                   }
   return xmlhttp;
   }
   </script>
   <?php

if ($pl['my_jail'] > gmtime()) {
    echo "<b>&gt;<a href='bust.php'>Try and escape for " . abs(intval($pl['my_maxnerve'] / 2)) . " nerve</a></b>
          <hr width='85%'>";
}

echo "<b></b> ";
if (mysql_num_rows($tot) <= '25') {
    echo "";
} else {
    if ($_GET['page'] > '1') {
        echo "<a href='jail.php?page=" . ($_GET['page'] - 1) . "'>&lt;&lt;</a> ";
    }
    for ($i = 0; $i < (mysql_num_rows($tot) / 25); $i++) {
        echo "<a href='jail.php?page=" . ($i + 1) . "'>";
        if (($i + 1) == $_GET['page']) {
            echo "<b>" . ($i + 1) . "</b>";
        } else {
            echo "<font color = '#999999'>" . ($i + 1) . "</font>";
        }
        echo "</a> ";
    }
    if ($_GET['page'] < $i) {
        echo " <a href='jail.php?page=" . ($_GET['page'] + 1) . "'>&gt;&gt;</a>";
        exit();
    }
}


echo "<table class='sidebarLink' border='0' width=95% class='rounded'><tr bgcolor=#151515>
         <td height='20'><font color = '#FFFFFF'><b>ID#</b></font></td>
         <td height='20'><font color = '#FFFFFF'><b>Player</b></font></td>
         <td width=25%><font color = '#FFFFFF'><b>Time</b></font></td>
         <td height='20'><font color = '#FFFFFF'><b>Level</b></font></td>
         <td height='20'><font color = '#FFFFFF'><b>Reason</b></font></td>
         <td height='20'><font color = '#FFFFFF'><b>Release</b></font></td></tr>";
$num = 0;

$q_ry = array();
$q_ry = "SELECT `playerid`,`playername`,`my_level`,`jail_offer`,`my_jail`,`jail_reason` 
                  FROM `members` 
                  WHERE `my_jail` > '" . mysql_real_escape_string(gmtime()) . "'
                  ORDER BY `my_jail` DESC
                  LIMIT $min,25";
$hopl = array();
$hopl = mysql_query($q_ry);
if (mysql_num_rows($hopl)) {
    $hp = array();
    while ($hp = mysql_fetch_array($hopl)) {
        $num++;
        if ($num % 2) {
            $color = "#E6E6E6";
        } else {
            $color = "#E6E6E6";
        }
        $q_ry       = array();
        $q_ry       = "SELECT `my_bustreward` 
                  FROM `members_extra` 
                  WHERE `playerid` = '" . $hp['playerid'] . "'";
        $du         = array();
        $du         = mysql_fetch_array(mysql_query($q_ry));
        $settime[] = $hp['playerid'] . ":" . $hp['my_jail'];

        echo "<tr bgcolor=#E6E6E6>
            <td><a href = 'messages.php?action=send&XID=" . $hp['playerid'] . "'>" . $hp['playerid'] . "</a></td>
            <td><a href = 'profile.php?XID=" . $hp['playerid'] . "'>" . htmlentities($hp['playername']) . "</a></td>
            <td><span id = 'time" . $hp['playerid'] . "'><b>" . gettimeleft($hp['my_jail']) . "</b></span></td>
            <td>" . $hp['my_level'] . "</td>
            <td>" . stripslashes($hp['jail_reason']) . "</td>
            <td>[<a href='release.php?action=bail&XID=" . $hp['playerid'] . "'>Bail</a>] 
                [<a href='release.php?action=bust&XID=" . $hp['playerid'] . "'>Bust</a>]</td></tr>";

    }
} else {
    echo "<tr>
               <td colspan = '7' align = 'center'>
               You walk into the jail to tease some inmates, but there are no inmates to tease!
               </td></tr>";
}
echo "</table>
               <hr width = '95%'>";
if ($pl['my_jail'] > gmtime() || $pl['am_i_staff'] > 4) {
    if ($pl['am_i_staff'] > 4) {
        echo '<table width="95%" align="center" border="0" bgcolor="#E6E6E6">
        <b>Clear the current shoutbox.</b>
        <form method="post" action="#"><tr>
        <td><input type="hidden" name="clear"></td>
        </tr><tr>
        <td colspan="2" valign="middle" align="center"><input class="hospchat" type="submit" value="Clear ALL Shouts"></td>
        <td></td>
        </tr></form></table><br />';
    }
    if (isset($_POST['clear'])) {
        if ($pl['am_i_staff'] < 5) {
            print "Sorry, staff only. <a href=jail.php>> back</a>.";
            exit();
        } else {
            mysql_query("TRUNCATE table `jailshoutsbox`");
            print "All of the jail shouts have been <b>cleared</b> <a href=jail.php>> back</a>.";
        }
    }
    if (isset($_POST['shout'])) {
        if ($pl['lastShoutj'] == date("i") && $pl['am_i_staff'] < 5 && $pl['my_dondays'] < 1) {
            echo "<div style='background: #DFDFDF;' width='85%'>Sorry, non donators can only post once per minute. <br /> <a href=jail.php> > back</a></div><br />";
            exit();
        }
        if ($pl['my_jail'] <= 0 && $pl['am_i_staff'] < 5) {
            echo "You are not in the jail. <a href=jail.php>> Back</a>";
            exit();
        }
        echo "<div style='background: #E6E6E6;' width='85%'>You've shouted<br /><a href=jail.php>Refresh</a></div><br />";

        $_POST['shout'] = htmlspecialchars(($_POST['shout']));

        $not            = array(
            "'",
            "/",
            "<",
            ">",
            ";"
        );
        $_POST['shout'] = str_replace($not, "", $_POST['shout']);


        mysql_query("INSERT INTO `jailshoutsbox` VALUES ('NULL', {$_SESSION['playerid']}, '{$_POST['shout']}', " . date("d") . ")");
    }


    echo '  <hr width=95% /> Post a message on the shoutbox.
        <table width="95%" align="center" border="0" bgcolor="#E6E6E6">
        <form method="post" action="#"><tr>
        <td>Your Message: (max 155) </td>
        <td><input class="hospchat" type="text" name="shout" maxlength="155"></td>
        </tr><tr>
        <td colspan="2" valign="middle" align="center"><input class="hospchat" type="submit" value="Shout"></td>
        <td></td>
        </tr></form></table><br /><table width="95%" style=text-align:left class="table2" border="0" cellspacing="2">
        <tr bgcolor="#151515" style="font-style:bold; text-align:center;"><td style="font-style:bold;" width=55%><b><font color="#FFFFFF">Posted By:</b></td><td style="font-style:bold;" width="44%"><b><font color="#FFFFFF">Messsage:</b></td></tr>
        ';

    $get = mysql_query("SELECT * FROM `jailshoutsbox` ORDER BY `ID` DESC LIMIT 10");
    while ($r = mysql_fetch_array($get)) {
        $num9  = $num9 + 1;
        $odd9  = "#CCCCCC";
        $even9 = "#e3e3e3";
        if ($num9 % 2) {
            $color9 = "$even9";
        } else {
            $color9 = "$odd9";
        }
        if ($r['User'] == 1) {
            $r['Shout'] = "<font color='blue'>" . $r['Shout'] . "</font>";
        }
        $user = mysql_query("SELECT `playername` FROM `members` WHERE `playerid`={$r['User']}");
        while ($user1 = mysql_fetch_array($user))
            $player = ($r['User'] == 0) ? "SYSTEM" : "<a href='profile.php?XID={$r['User']}'>[{$r['User']}] {$user1['playername']}</a>";
        echo "<tr height='50px' bgcolor=#E6E6E6><td>$player</td><td style='text-align:center;'>{$r['Shout']}</td></tr>";
    }
    echo "</table>";
}


include('./includes/style_bottom.php');
?>

这是gettimeleft函数。

function gettimeleft($tl) {
                  if($tl <= time()) { $release = "0 Seconds"; }
                  else
                  {
                  $mins = floor(($tl - time()) / 60);
                  $hours = floor($mins / 60);
                  $mins -= $hours * 60;
                  $days = floor($hours / 24);
                  $hours -= $days * 24;
                  $months = floor($days / 31);
                  $days -= $months * 31;
                  $weeks = floor($days / 7);
                  $days -= $weeks * 7;
                  $timeleft = ($tl - time());
                  $secs = round($timeleft%60);
                              if ($months > 0)//MONTHS
                                 {
                                  $release .= " $months Month" . ($months > 1 ? "s" : "");
                                 }
                              if ($weeks > 0)//WEEKS
                                 {
                              if ($months > 0)
                                 {
                                  $release .= ",";
                                 }
                                  $release .= " $weeks Week" . ($weeks > 1 ? "s" : "");
                                 }
                              if ($days > 0)//DAYS
                                 {
                              if ($months > 0 ||$weeks > 0)
                                 {
                                  $release .= ",";
                                 }
                                  $release .= " $days Day" . ($days > 1 ? "s" : "");
                                 }
                              if ($hours > 0)//HOURS
                                 {
                              if ($months > 0 ||$weeks > 0 || $days > 0)
                                 {
                                  $release .= ",";
                                 }
                                  $release .= " $hours Hour" . ($hours > 1 ? "s" : "");
                                 }
                              if ($mins > 0)//MINUTES
                                 {
                              if ($months > 0 ||$weeks > 0 || $days > 0 || $hours > 0)
                                 {
                                  $release .= ",";
                                 }
                                  $release .= " $mins Minute" . ($mins > 1 ? "s" : "");
                                 }
                              if($secs > 0)//SECONDS
                              {
                                  if($release != "")
                                  {
                                    $release .= " and";
                                  }
                                  $release .= " $secs Second" . ($secs > 1 ? "s" : "");
                              }
                              }
                       return $release;
                   }

是否有人能帮助我解决这个问题?

0 个答案:

没有答案