因此,我尝试为我的脚本做一个计时器,但它只对最后一个结果进行倒计时,而不是对所有结果进行倒计时,并且对于从查询返回的所有结果,都显示了从最后一个结果开始的倒计时。任何帮助都将不胜感激。谢谢
<?php
$q = $handler->prepare("SELECT * FROM pending_payments WHERE user_id = ? AND status = ?");
$q->bindParam(1, $session_id);
$q->bindParam(2, $pending);
$q->execute();
if($q->rowCount() > null){
?>
<h4 class="title"> Pending Payment </h4>
<div class="squiggly-border"></div>
<table class="table table-index">
<thead>
<tr>
<th>#</th>
<th>Item</th>
<th>Expires</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
while($r = $q->fetch()) {
$pending_payment_id = $r['id'];
$pending_item = $r['item'];
$pending_expiry = $r['expiry'];
?>
<script type="text/javascript">
var count = <?php if($dateUNIX < $pending_expiry){ echo $pending_expiry - $dateUNIX; } ?>;
var counter = setInterval(timer, 1000); //1000 will run it every 1 second
function timer() {
count = count - 1;
if (count == -1) {
clearInterval(counter);
return;
}
var seconds = count % 60;
var minutes = Math.floor(count / 60);
var hours = Math.floor(minutes / 60);
minutes %= 60;
hours %= 60;
document.getElementById("pEFTimer<?php echo $r['id'] ?>").innerHTML = hours + " hours " + minutes + " minutes "; // watch for spelling
}
</script>
<tr>
<td class="text"><?php echo $pending_payment_id; ?></td>
<td class="text"><?php echo $pending_item ?></td>
<td class="text" id="pEFTimer<?php echo $r['id'] ?>"></td>
<td class="text"><a href="pendingPayment<?php echo $r['id']; ?>" ><button class="btn btn-success btn-xs">Proceed to Payment</button></a></td>
</tr>
<?php
}
echo '</tbody></table>';
}
?>发布于 2015-03-18 05:52:25
在javascript中,函数是执行某些代码的变量,所以基本上不是为每一行创建一个函数,而是将函数更新为只跟踪最后一个元素。
让我们弄清楚每种语言工作的地方,PHP在服务器中输出您的html,html由浏览器解析,然后Javascript在浏览器(客户端)中执行。
也就是说,解决这个问题的最好方法是将数据作为数据属性存储在超文本标记语言中的每一行的特定元素上,使用JavaScript进行检索,然后启动一个同时更新所有元素的超时。
https://stackoverflow.com/questions/29110259
复制相似问题