我正在尝试为 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
?>