我试图从数据库创建倒数计时器。我已将 deltaTimeServer 发送到 JS。输出是正确的,但它们冻结(不是倒计时,我必须按 F5)。对我有什么想法吗?
这是我的代码。
JS
<script type="text/javascript">
function countDown(){
$(".show").each(function() {
var elm = $(this);
var difTime=this.timestamp;
var day=0,hours=0,minutes=0,seconds=0;
if(difTime>0){
day=Math.floor(difTime/84600);
hours=(Math.floor((difTime/3600))%24) + day*24 ;
minutes=Math.floor(difTime/60)%60;
seconds=Math.floor(difTime)%60;
}
else{
elm.removeClass("show"); //for remove class show
}
elm.html(hours+' H '+minutes+' M '+seconds+' S ');
});
}
function countDown_onLoad(){
$(".show").each(function() {
this.timestamp = parseInt(this.firstChild.nodeValue,10);
});
setInterval(countDown,1000);
}
$(document).ready(function() {
countDown_onLoad();
});
</script>
PHP
$show=mysql_query("SELECT * FROM `room_lists` WHERE `active` = 1");
while ($array = mysql_fetch_array($show))
{
$timeStop = $array['timeStop'];
$deltaTimeServer = strtotime($timeStop)-strtotime(date('Y-m-d H:i:s'));
echo "<td align = 'center'><div class=\"show\">".$deltaTimeServer."</div></td>";
}