码迷,mamicode.com
首页 > Web开发 > 详细

jquery时间倒计时

时间:2016-02-23 18:37:45      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

代码:

js:

function countDown(time, id) {  //time的格式yyyy/MM/dd hh:mm:ss
    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
    sys_second = (end_time - new Date().getTime()) / 1000;
    var timer = setInterval(function () {
        if (sys_second > 1) {
            sys_second -= 1;
            var day = Math.floor((sys_second / 3600) / 24);
            var hour = 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);
        }
    }, 1000);
}

 

html:

  <p id="colockbox">
              <span class="day" id="day">00</span>

     <span class="hour">00</span>:

              <span  class="minute">24</span>:

     <span class="second">24</span>
      </p>

jquery时间倒计时

标签:

原文地址:http://www.cnblogs.com/linJie1930906722/p/5210867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!