实现目标
源码:
<p>测试倒计时:<span id="payResetTimeShow" style="color:red"></span></p>
$(function(){
var times=18000;
if(times>=0){
countdown(times);
}
});
function countdown(times){
var timer=null;
$('#payResetTimeShow').text(getRestTime(times));
timer=setInterval(function(){
times--;
$('#payResetTimeShow').text(getRestTime(times));
if(times<=0){
clearInterval(timer);
$('#payResetTimeShow').text('已停止');
}
},1000);
}
function getRestTime(times){
var day=0,hour=0,minute=0,second=0;//时间默认值
if(times > 0){//s
day = Math.floor(times/3600/24);
hour = Math.floor(times/3600%24);
minute = Math.floor(times/60%60);
second = Math.floor(times%60);
}
day = formatTime(day);
hour = formatTime(hour);
minute = formatTime(minute);
second = formatTime(second);
var textHtml='';
if(parseInt(day)>0){
textHtml+=day+'天';
}
textHtml+=hour+'小时'+minute+'分'+second+'秒';
return textHtml;
}
function formatTime(times){
if(times<=9){
return times = '0' + times;
}
return times;
}