js倒计时以及ie兼容问题

时间:2021-02-10 22:02:49

前两天找了一段倒计时的代码,整了半天才弄好,现在弄成一个接口,方便日后查看!

其中,id是显示时间的文本的id,time_end和time_start就不解释了咯!

var timerID = null;
var time_now_client=new Date().getTime();
function show_time(id, time_end,time_start){
	
	 var timer = document.getElementById(id);
	 var time_server_client=time_start-time_now_client;
	 if(!timer){return;}
	 timer.innerHTML =time_server_client;
	 var time_now,time_distance,str_time;
	 var int_day,int_hour,int_minute,int_second;
	 var time_now=new Date();
	 time_now=time_now.getTime()+time_server_client;
	 time_distance=time_end-time_now;
	 if(time_distance>0){
	      int_day=Math.floor(time_distance/86400000)
	      time_distance-=int_day*86400000;
	      int_hour=Math.floor(time_distance/3600000)
	      time_distance-=int_hour*3600000;
	      int_minute=Math.floor(time_distance/60000)
	      time_distance-=int_minute*60000;
	      int_second=Math.floor(time_distance/1000)
	      if(int_hour<10)
	       int_hour="0"+int_hour;
	      if(int_minute<10)
	       int_minute="0"+int_minute;
	      if(int_second<10)
	       int_second="0"+int_second;
	       str_time=int_day+"天"+int_hour+"小时"+int_minute+"分钟"+int_second+"秒";
	       timer.innerHTML=str_time;
	       
	       setTimeout(function(){
				show_time(id,time_end, time_start)
			},1000);
     }else{
        timer.innerHTML =timer.innerHTML;
     }   
} 

然后下面这个就是调用的代码

var raiseEnd = '${financingDetailVo.raiseEnd?string("yyyy/MM/dd HH:mm:ss")}';
var raiseStart = '${financingDetailVo.raiseStart?string("yyyy/MM/dd HH:mm:ss")}';
if(2 == ${sell.getStatus()}){
	setTimeout(function(){
		show_time("restTime2",new Date(raiseEnd).getTime(), new Date().getTime())
	},1000);
}else if(1 == ${sell.getStatus()}){
	setTimeout(function(){
		show_time("restTime3",new Date(raiseStart).getTime(), new Date().getTime())
	},1000);
}

这里有个关键的就是兼容性的问题,用new Date()传值在ie中会有兼容问题..

 所有任何时候传进去的值都要是("yyyy/MM/dd HH:mm:ss")的格式

不能是("yyyy-MM-dd HH:mm:ss")什么的.(这玩意弄了好久..)


大概就这样了.后面用的时候可以直接拿过来用