我一直在为这个倒数计时器苦苦挣扎。我已经取得了很大的进步,但由于某种原因,当分钟和秒数为0时,它并没有减少时间。这是为什么?
代码:
<?php
$timeTo = strtotime(gmdate("12:24:00")).'<br />';
$timeNow = strtotime('now').'<br />';
$differenceInSeconds = $timeTo - $timeNow;
?>
<script type="text/javascript">
var s= "<?php Print($differenceInSeconds);?>";
var h= Math.floor(s/3600);
s-= h*3600;
var m= Math.floor(s/60);
s -= m*60;
var counter=setInterval(timer, 1000); //1000 will run it every 1 second
function timer()
{
s=s-1;
if(s == 0 ){
m=m-1;
s=59;
if(s == 0 && m == 0){
h=h-1;
s=59;
m=59;
if(s == 0 && m == 0 && h == 0){
// reset the counter
//return;
}
}
}
//Do code for showing the number of seconds here
document.getElementById("timer").innerHTML=h+'hrs '+m+'min '+s+'secs ';//(h <= 0 ? ' ' : h+"hr ")+(m <= 0 ? ' ' : m+"min ")+(s < 10 ? '0'+s+'secs ' : s+"secs "); // watch for spelling
}
</script>
<span id="timer"></span>
继承人JSFiddle
答案 0 :(得分:1)
首先检查s == 0
是否然后递减,而不是按相反的顺序递减:
s=s-1; // s will be -1
if(s == 0 ){
但是
if (s == 0) {
// logic here
} else {
s = s - 1;
}