js倒计时,谁知道怎么把倒计时到一位数的时候前面加个0啊?急~~
发布网友
发布时间:2022-04-24 05:04
我来回答
共2个回答
热心网友
时间:2022-04-24 06:34
<p id="times"></p>
<script type="text/javascript">
function charLeftAll(n)
{
if(n < 10)
return "0" + n;
else
return n;
}
function fresh()
{
var endtime=new Date("2110/11/06,23:20:12");
var nowtime = new Date();
var leftsecond=parseInt((endtime.getTime()-nowtime.getTime())/1000);
d=parseInt(leftsecond/3600/24);
h=parseInt((leftsecond/3600)%24);
m=parseInt((leftsecond/60)%60);
s=parseInt(leftsecond%60);
// document.getElementById("times").innerHTML=__h+"小时"+__m+"分"+__s+"秒";
document.getElementById("times").innerHTML=charLeftAll(h)+"时"+charLeftAll(m)+"分"+charLeftAll(s)+"秒";
if(leftsecond<=0){
document.getElementById("times").innerHTML="应征时间已结束";
clearInterval(sh);
}
}
fresh()
var sh;
sh=setInterval(fresh,1000);
</script>
热心网友
时间:2022-04-24 07:52
document.getElementById("times").innerHTML=h+"时"+m+"分"+s+"秒";
改成
document.getElementById("times").innerHTML=hh+"时"+mm+"分"+ss+"秒";
简单吧 哈哈