js实现倒计时

来源:互联网 发布:mac chrome 导出插件 编辑:程序博客网 时间:2024/06/18 15:29
 $(function () {
            var sec_str = "<%=promotion_second %>";
            if (parseInt(sec_str) < 0) {
                timer(sec_str.substring(1));
            }
            else {
                timer(sec_str);
            }
        });
        //倒计时
        function timer(intDiff) {
            $('#hour_show').html("");
            $('#minute_show').html("");
            $('#second_show').html("");
            time_str = window.setInterval(function () {
                var day = 0,
        hour = 0,
        minute = 0,
        second = 0;
                if (intDiff > 0) {
                    hour = Math.floor(intDiff / 3600);
                    minute = Math.floor((intDiff % 3600) / 60);
                    second = Math.floor(intDiff % 60);
                }
                if (hour <= 9) hour = '0' + hour;
                if (minute <= 9) minute = '0' + minute;
                if (second <= 9) second = '0' + second;
                $('#hour_show').html(hour);
                $('#minute_show').html(minute);
                $('#second_show').html(second);
                intDiff--;
            }, 1000);
        }