倒计时,用于限时优惠

Posted chenyiteng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了倒计时,用于限时优惠相关的知识,希望对你有一定的参考价值。

//倒计时
$(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);
}

以上是关于倒计时,用于限时优惠的主要内容,如果未能解决你的问题,请参考以下文章

抢TWeb大会线上直播优惠购票,倒计时1天!!

限时抢购-倒计时

限时购--倒计时?

iOS 时间校准解决方案

js实现-限时抢购倒计时间表

如何卡掉传奇私服进图倒计时