之前的倒计时是这么写的
//计算剩余时间 time-截止时间;serverTime--服务器时间,id-要显示时分秒对应的父节点Id
function countRestTime(time,serverTime,id){
var day_elem = $(id).find('.day');
var hour_elem = $(id).find('.hour');
var minute_elem = $(id).find('.minute');
var second_elem = $(id).find('.second');
var end_time = new Date(time).getTime();//月份是实际月份-1
var start_time= new Date(serverTime).getTime();
var sys_second = (end_time-start_time)/1000;
function startTime(){
if (sys_second > 1) {
sys_second -= 1;
var day = Math.floor((sys_second / 3600) / 24);
var hour = day*24 + Math.floor((sys_second / 3600) % 24);
var minute = Math.floor((sys_second / 60) % 60);
var second = Math.floor(sys_second % 60);
day_elem && $(day_elem).text(day);//计算天
$(hour_elem).text(hour<10?"0"+hour:hour);//计算小时
$(minute_elem).text(minute<10?"0"+minute:minute);//计算分钟
$(second_elem).text(second<10?"0"+second:second);//计算秒杀
} else {
clearInterval(timer);
}
};
timer = setInterval(startTime,1000);
startTime();
}
以上代码在google浏览器,安卓手机微信是没任何问题的,但是在苹果手机或者苹果微信异常;
问题出在new Date(time).getTime();返回的是NaN;
解决方案:
new Date(time).getTime();返回的是NaN;
改为:
eval("new Date("+ time.replace(/\D+/g,",")+")").getTime();
则在google浏览器 安卓、安卓微信,苹果浏览器 ,苹果微信统统秒杀!
//计算剩余时间
function countRestTime(time,serverTime,id){
var day_elem = $(id).find('.day');
var hour_elem = $(id).find('.hour');
var minute_elem = $(id).find('.minute');
var second_elem = $(id).find('.second');
var end_time = eval("new Date("+ time.replace(/\D+/g,",")+")").getTime();
var start_time= eval("new Date("+ serverTime.replace(/\D+/g,",")+")").getTime();
var sys_second = (end_time-start_time)/1000;
function startTime(){
if (sys_second > 1) {
sys_second -= 1;
var day = Math.floor((sys_second / 3600) / 24);
var hour = day*24 + Math.floor((sys_second / 3600) % 24);
var minute = Math.floor((sys_second / 60) % 60);
var second = Math.floor(sys_second % 60);
day_elem && $(day_elem).text(day);//计算天
$(hour_elem).text(hour<10?"0"+hour:hour);//计算小时
$(minute_elem).text(minute<10?"0"+minute:minute);//计算分钟
$(second_elem).text(second<10?"0"+second:second);//计算秒杀
} else {
clearInterval(timer);
}
};
timer = setInterval(startTime,1000);
startTime();
}