二个简单的倒计时代码(剩余时间与倒计时)

发布时间:2020-09-05编辑:脚本学堂
本文提供二个有关js倒计时的例子,一个是计算剩余时间,一个是简单的倒计时功能,有需要的朋友,可以参考下。

1、计算剩余时间:

<i id="expireTime"></i>
<script type="text/javascript">
 var expire = 2412169;
 var expireTime = function(expires){
  if(expires > 0){
   var second = expires;
  }else{
   var second = "0 分";
   return second;
  }
  var day = hour = min = "";
  if(second>86400){
   day = Math.floor(second/86400)+"天 ";
   second = second%86400;
  }
  if(second>3600){
   hour = Math.floor(second/3600)+"时 ";
   second = second%3600;
  }
  if(second>60){
   min = Math.floor(second/60)+"分 ";
   second = second%60;
  }
  second = second+"秒";
  return day+hour+min+second;
 }
 
 var timeEle = document.getElementById("expireTime");
 
 var timer = window.setInterval(function(){
  timeEle.innerHTML = expireTime(expire--);
  if(expire<0){
   clearInterval(timer);
  }
 },1000);
</script>