js计时器

来源:互联网 发布:js防水涂料是什么意思 编辑:程序博客网 时间:2024/04/28 20:26

计时器


第一种:根据页面时间倒计时
(function(){    var startCountDown = function(){        var hours = parseInt($("#J_hour").text());        var minutes = parseInt($("#J_minute").text());        var seconds = parseInt($("#J_second").text());        if (seconds <= 0) {            if (minutes <= 0) {                if (hours <= 0) {                    alert("时间结束!");                    return ;                } else {                    hours--;                }                minutes = 59;            } else {                minutes--;            }            seconds = 59;        } else {            seconds--;        }        $("#J_second").html((seconds).toString().length > 1 ? seconds : "0" + seconds);        $("#J_minute").html((minutes).toString().length > 1 ? minutes : "0" + minutes);        $("#J_hour").html((hours).toString().length > 1 ? hours : "0" + hours);        time = setTimeout(startCountDown, 1000);    };    window.startCountDown = startCountDown;})();

第二种:js指定时间倒计时
(function(){    var seconds = 60; // 可配置    var startCountDown = function(){        if (seconds <= 0) {            console.log("获取超时!");        } else {            console.log("请耐心等待(" + (seconds--) + "s)");        }        time = setTimeout(startCountDown, 1000);    };    window.startCountDown = startCountDown;})();
0 0
原创粉丝点击