zoukankan      html  css  js  c++  java
  • JS倒计时 代码

    JS倒计时 代码

    <div>
    	<span id="KSD">3</span>天
    	<span id="KSH">12</span>小时
    	<span id="KSM">39</span>分钟
    	<span id="KSS">25</span>秒
    </div>
    
    
    <script type="text/javascript">
    function countDown(targetTime, callback) {
    	var t_timestamp = Date.parse(targetTime);
    	var s_timestamp = new Date();
    	c_timestamp = t_timestamp - s_timestamp; // 倒计时间戳
    	if (c_timestamp > 0) {
    		setTimeout(function callee() {
    			countdownTime(c_timestamp);
    			if (c_timestamp > 0) {
    				c_timestamp -= 1000;
    				setTimeout(callee, 1000);
    			}
    		}, 1);
    	}
    
        // 计算倒计时间(天,小时,分钟,秒),并传入回调函数,执行回调
        function countdownTime(c_timestamp) {
            var d, h, m, s;
            c_timestamp = c_timestamp / 1000;
    
            d = parseInt(c_timestamp / 3600 / 24, 10); // 天数
            h = parseInt(c_timestamp / 3600 % 24, 10); // 小时
            m = parseInt(c_timestamp % 3600 / 60, 10); // 分钟
            s = parseInt(c_timestamp % 3600 % 60, 10); // 秒
    
            if (typeof callback === 'function') {
                callback(d, h, m, s);
            }
        }
    };
    
    var targetTime = '2013/11/12 00:00:00'; // 大于本地时间(假如本地时间为:2013/3/14 16:10:00)
    countDown(targetTime, function(d, h, m, s) {
    
        // 补零
        for (var i = 0, len = arguments.length; i < len; i++) {
            if (String(arguments[i]).length < 2) {
                arguments[i] = '0' + arguments[i];
            }
        }
        // dom操作
        document.getElementById('KSD').innerHTML = h;
        document.getElementById('KSH').innerHTML = h;
        document.getElementById('KSM').innerHTML = m;
        document.getElementById('KSS').innerHTML = s;
    });
    </script>
    

      

  • 相关阅读:
    linux服务器网络配置
    全面了解linux服务器
    centos selinux学习记录
    centos7使用samba共享文件
    centos7修改yum下载源为阿里源
    ubuntu14.04使用samba共享文件
    计算两个经纬度之间的距离(python算法)
    awk中的冒泡排序
    linux awk时间计算脚本
    linux shell中FS、OFS、RS、ORS图解
  • 原文地址:https://www.cnblogs.com/didi/p/3418356.html
Copyright © 2011-2022 走看看