我正在从db获取的动态数据数组上设置一个计时器。它可以正常工作但是当我第二次单击该按钮时,它会非常快速地增加秒数。我还想在每次单击按钮时将初始计数设置回60。有人可以帮忙吗?到目前为止,我的代码如下:
<script>
var counts = [];
var xs = [];
for (var i = 1; i < 61; i++) {
counts.push(61);
xs.push(0);
};
function timer(id) {
xs[id] = setTimeout("timer(" + id + ")", 1000);
counts[id] = counts[id] - 1;
if (counts[id] < 1) { counts[id] = 0; }
document.getElementById("but" + id).innerHTML = counts[id];
}
function restarttimer(id) {
xs[id] = setTimeout("timer(" + id + ")", 1000);
counts[id] = counts[id] - 1;
if (counts[id] < 1) { counts[id] = 0; }
document.getElementById("but" + id).value = counts[id];
}
</script>
<?php
$i=1;
?>
<table id="itemTable"><tr>
<?php
while($r=mysql_fetch_array($resultSearch))
{
?>
<td>
<p id="but<?php echo $r['itemID'];?>">>
</p>
<input type="button" value="bid" onclick="timer(<?php echo $r['itemID'];?>)" />
</td>
<?php
if($i % 3 == 0)
echo '</tr><tr>';
$i++;
}
&GT?;