前端时间倒计时方案

Posted on 2018-06-12 11:32  李太阳  阅读(1803)  评论(0编辑  收藏  举报

 

<script>
var i=0;
function showtime(){

//需要倒计时的时间节点
var gameTime = $("#gameTime").val();
if(gameTime ==null || gameTime==""){
return false;
}
var endyear=gameTime.substring(0,4);
var endmonth=gameTime.substring(5,7);
var endday=gameTime.substring(8,10);

//竞猜时间截止于开赛前1小时;
var endh=gameTime.substring(11,13);
var endms=gameTime.substring(13,19);

var endhms=endh+endms;
//后台传过来的当前系统时间
var sTime = new Date('${sTime}');
var enddate=endyear+"/"+endmonth+"/"+endday+" "+endhms;
var eTime=new Date(enddate);

i=i+1;
var sumSecond=parseInt((eTime.getTime()-(sTime.getTime()+(i*1000)))/1000);
var showSecond=sumSecond%60;
var sumMinute=(sumSecond-showSecond)/60;
var showMinute=sumMinute%60;
var sumHour=(sumMinute-showMinute)/60;
var showHour=sumHour%24;
var showData=(sumHour-showHour)/24;
if(showSecond>-1){
$("#showday").html(showData);
$("#showhour").html(showHour);
$("#showminute").html(showMinute);
$("#showsecond").html(showSecond);

//循环此方法
timerID=setTimeout("showtime()",1000);
}else{
$("#time").html("距离竞猜结束:<span>已结束</span>");
}
}
</script>