带有PHP数组的Javascript倒计时器

时间:2013-10-23 12:47:16

标签: javascript php timer

我正在尝试为PHP数组中的每个结果创建倒数计时器,其中一个单击链接并且它分别跟踪每个计时器。到目前为止,我有它为第一个结果工作,但由于我正在通过ID抓取元素,我遇到了问题。我意识到我需要按阶段获取元素,但我仍然太吵闹,这就是我到目前为止所做的。

此外,如果您多次点击链接,倒计时每次都会自动增加。点击后我可以隐藏链接,但可能有更好的解决方案。我确定代码很乱,而且我可能会完全错误!谢谢你的帮助。

    <script>
var mins = 10;
var secs = 0;
var timer;

function start(){
    timer = setInterval('update()', 1000);
        }

function update(){
    var timeField = document.getElementById('time');
    if(secs == 0){
        if(mins == 0){
    timeField.innerHTML = "Ready";
    clearInterval(timer);
    return;
                        }
mins--;
secs=59;
                    } 
else {
    secs--;
        }
    if(secs<10){
    timeField.innerHTML = mins + ":0" + secs;
    } 
    else {
    timeField.innerHTML = mins + ":" + secs;
    }
}
</script>

PHP:

    <?php
require ('info.php');
// database connection info
$conn = mysql_connect($sql_server,$username,$password) or trigger_error("SQL", E_USER_ERROR);

$db = mysql_select_db($database,$conn) or trigger_error("SQL", E_USER_ERROR);
$i = 0;
$sql = "SELECT COUNT(*) FROM faucet";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$sql = "SELECT * FROM faucet WHERE time BETWEEN 0 AND 29 ORDER BY faucet ASC";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

    echo "<div style=\"margin:5px 0 5px 0;\"><h3>Continuous and Other Faucets</h3></div>";
    echo "<div id=\"container\" style=\"clear:both; margin-top:10px;\"><div id=\"row\"><div id=\"left\" class=\"faucetg\">Faucet</div><div id=\"middle\" class=\"faucetg\">Dispense</div><div id=\"middle\" class=\"faucetg\">Time</div><div id=\"middle\" class=\"faucetg\">Type</div><div id=\"right\" class=\"faucetg\">Status</div></div></div>";

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
    $dmin = $list['dmin']/100;
    $dmax = $list['dmax']/100;
   // echo data
    if ($i&1) {
echo "<div id=\"container\" style=\"clear:left;\"><div id=\"row\"><div id=\"left\" class=\"faucet\"><a onclick=\"start()\" target=\"_blank\" href=\"" . $list['url'] . "\">" . $list['faucet'] . "</a></div><div id=\"middle\" class=\"faucet\">" . $dmin . " - " . $dmax . " uBTC</div><div id=\"middle\" class=\"faucet\">" . $list['time'] . " minutes</div><div id=\"middle\" class=\"faucet\">" . $list['type'] . "</div><div id=\"right\" class=\"faucet\">" . $list['status'] . "<span id=\"time\" style='float:right;'>Ready</span></div></div></div>";
}
else {
echo "<div id=\"container\" style=\"clear:left;\"><div id=\"row\"><div id=\"left\" class=\"faucetl\"><a onclick=\"start()\" target=\"_blank\" href=\"" . $list['url'] . "\">" . $list['faucet'] . "</a></div><div id=\"middle\" class=\"faucetl\">" . $dmin . " - " . $dmax . " uBTC</div><div id=\"middle\" class=\"faucetl\">" . $list['time'] . " minutes</div><div id=\"middle\" class=\"faucetl\">" . $list['type'] . "</div><div id=\"right\" class=\"faucetl\">" . $list['status'] . "<span id=\"time\" style='float:right;'>Ready</span></div></div></div>";
}  
  $i++;
} // end while
?>

0 个答案:

没有答案