码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript 的倒计时

时间:2016-08-05 22:56:44      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

一年前,在网上找到的例子,现在已经找不到出处,对不住原作者,请原谅。修改了一下,在刷新页面的情况下,倒计时不重来。

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>倒计时</title>
</head>
<body>
<label id="time"></label>
<script>
    function CountDown() {
        var localtime=sessionStorage.getItem(‘time‘);
        if(localtime){
            if (localtime >= 0) {
                hours=Math.floor(localtime/3600);
                minutes = Math.floor((localtime-hours*3600)/ 60);
                seconds = Math.floor((localtime-hours*3600) % 60);
                hours = hours >= 10 ? hours : ‘0‘ + hours;
                minutes = minutes >= 10 ? minutes : ‘0‘ + minutes;
                seconds = seconds >= 10 ? seconds : ‘0‘ + seconds;
                msg = "距离结束还有"+ hours + "时" + minutes + "分" + seconds + "秒";
                document.getElementById(‘time‘).innerHTML = msg;
                if (localtime == 5*60) alert(‘注意,还有5分钟!‘);
                --localtime;
                sessionStorage.setItem(‘time‘,localtime)
            }
            else {
                document.getElementById(‘time‘).innerHTML = "时间到,结束!";
            }
        }else{
            var maxtime = 90*60; //一个半小时,按秒计算,自己调整!
            if (maxtime >= 0) {
                hours=Math.floor(maxtime/3600);
                minutes = Math.floor((maxtime-hours*3600)/ 60);
                seconds = Math.floor((maxtime-hours*3600) % 60);
                hours = hours >= 10 ? hours : ‘0‘ + hours;
                minutes = minutes >= 10 ? minutes : ‘0‘ + minutes;
                seconds = seconds >= 10 ? seconds : ‘0‘ + seconds;
                msg = "距离结束还有"+ hours + "时" + minutes + "分" + seconds + "秒";
                document.getElementById(‘time‘).innerHTML = msg;
                --maxtime;
                sessionStorage.setItem(‘time‘,maxtime)
            }
            else {
                document.getElementById(‘time‘).innerHTML = "时间到,结束!";
            }
        }
    }
    timer = setInterval("CountDown()", 1000);
</script>
</body>
</html>

 

JavaScript 的倒计时

标签:

原文地址:http://www.cnblogs.com/cyfhykx/p/5742823.html

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