js实现的倒计时效果(setInterval) - 环宇科讯|沃玛互动
致力于高端网站建设服务的专业公司
网站建设热线:010-51000596
首页 > 网站资讯 > 网站建设 > js实现的倒计时效果(setInterval)

js实现的倒计时效果(setInterval)

用JS实现的倒计时效果

  <div id="leftTime"></div>

 

  <script type="text/javascript">

  setInterval(function time(){

                                       var o  = document.getElementById("leftTime");

                                    

                                       var nowTime = new Date(),endTime = new Date("2012/7/14 18:00:00");

                                    

                                       var leftSecs = Math.floor((endTime.getTime() - nowTime.getTime()) / 1000);

                                    

                                       var leftDays = Math.floor(leftSecs / (24*60*60));       //剩余的天数

                                    

                                       var leftHour = Math.floor(leftSecs % (24*60*60) / (60*60));      //剩余的小时数

                                    

                                       var leftMins = Math.floor(leftSecs %s (60*60) / 60);  //剩余的分钟数

                                    

                                       var leftSec = Math.floor(leftSecs % 60);      //剩余的秒数

 

  o.innerHTML = "现在还剩下" + leftDays + "天" + leftHour + "小时" + leftMins + "分钟" + leftSec + "秒";

  },1000);

  </script>

 

当然写法不只这一种,还可以这样写:

setInterval("doTimeout()" , 1000); //true
setInterval(doTimeout() , 1000);     //false
setInterval(function() {           //truedoTimeout();
} , 5000);
setInterval(doTimeout , 1000);      //true

setInterval('doTimeout' , 1000);     //false

 

转载请注明出处:www.bsweb.cn/