1. 程式人生 > >倒計時,用於限時優惠

倒計時,用於限時優惠

.text get tex minutes interval 效果 直接 show 計時

//倒計時
$(document).ready(function(){
var doing_enddate = "2018/04/16 18:00:00";//正在倒計時
var start_enddate = "2018/09/09 09:00:00";//即將開始倒計時
run(doing_enddate,‘doing_time‘);
run(start_enddate,‘start_time‘);
run(doing_enddate,‘doing_time_up‘);
run(start_enddate,‘start_time_up‘);
});
function run(enddate,dateshowhtml){//dateshowhtml 在哪裏展示倒計時必須是id
//如果enddate為後臺傳入的Date類型,這裏直接轉化為毫秒數
enddate=new Date(enddate).getTime();
//以500毫秒的速度執行(可以避免方法執行速度慢會影響展示效果的情況)0
var time = 500;
setInterval("dateDif(‘"+enddate+"‘,‘"+dateshowhtml+"‘)",time);
}
//計算時間相差
function dateDif(enddate,dateshowhtml){
var date = enddate - new Date().getTime();
var days = date / 1000 / 60 / 60 / 24;
var daysRound = Math.floor(days) > 0 ? Math.floor(days) : ‘‘;
var hours = date/ 1000 / 60 / 60 - (24 * daysRound);
//hours = hours < 10 ? ‘0‘+hours : hours;
var hoursRound = Math.floor(hours) < 10 ? ‘0‘+Math.floor(hours) : Math.floor(hours);
var minutes = date / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);
//minutes = minutes < 10 ? ‘0‘+minutes : minutes;
var minutesRound = Math.floor(minutes) < 10 ? ‘0‘+Math.floor(minutes) : Math.floor(minutes);
var seconds = date/ 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
//seconds = seconds < 10 ? ‘0‘+seconds : seconds;
var secondsRound = Math.floor(seconds) < 10 ? ‘0‘+Math.floor(seconds) : Math.floor(seconds);
if(daysRound>0){
var time = daysRound+‘.‘+hoursRound+‘:‘+minutesRound+‘:‘+secondsRound;
}else{
var time = hoursRound+‘:‘+minutesRound+‘:‘+secondsRound;
}
$(‘#‘+dateshowhtml).text(time);
}

倒計時,用於限時優惠