简体   繁体   English

静态重复倒数

[英]Static Repeating Countdown

I've currently got a page getting fed information from a MySQL database that is updated by cron every 5 minutes 我目前有一个页面,该页面从MySQL数据库获取联合信息,该数据库由cron每5分钟更新一次

I would like to get the page refreshed every five minutes but also show a visible countdown to the users to the "Next Update" 我想每五分钟刷新一次页面,但还要向用户显示“下一次更新”的可见倒计时

Below is the code that I am currently using, but im not sure on how to make it static - as at the moment when a user refreshes the page it just restarts the countdown. 下面是我当前正在使用的代码,但是我不确定如何使其静态化-因为当用户刷新页面时,它只是重新开始倒计时。

I would appreciate any help given, even just to know where to start. 我将不胜感激,即使只是知道从何开始。

Thanks 谢谢

    <script>
    var Timer;
    var TotalSeconds;

    function CreateTimer(TimerID, Time) {
        Timer = document.getElementById(TimerID);
        TotalSeconds = Time;
        UpdateTimer()
        window.setTimeout("Tick()", 1000);
    }

    function Tick() {
        if (TotalSeconds <= 0) {
            Timer.innerHTML = "  Time's up  ";
            window.setTimeout("Tick", 1000);
            CreateTimer("timer", 5)
            return;
        }

        TotalSeconds -= 1;
        UpdateTimer()
        window.setTimeout("Tick()", 1000);
    }

    function UpdateTimer() {
        var Seconds = TotalSeconds;
        var Days = Math.floor(Seconds / 86400);
        Seconds -= Days * 86400;

        var Hours = Math.floor(Seconds / 3600);
        Seconds -= Hours * (3600);
        var Minutes = Math.floor(Seconds / 60);
        Seconds -= Minutes * (60);
        var TimeStr = ((Days > 0) ? Days + " days " : "") + LeadingZero(Hours) + ":" + LeadingZero(Minutes) + ":" + LeadingZero(Seconds);
        Timer.innerHTML += "    " + TotalSeconds;
        Timer.innerHTML = TimeStr;
    }

    function LeadingZero(Time) {
        return (Time < 10) ? "0" + Time : +Time;
    }
</script>
<div id="timer" >
<script type="text/javascript">window.onload = CreateTimer("timer", 5);</script>
</div >

use beforeunload to store current state in a cookie, then proceed form there. 使用beforeunload将当前状态存储在cookie中,然后从那里继续执行表单。

here is an example (and more elegant timer :-)) 这是一个示例(还有更优雅的计时器:-)

<html>
<head>
    <title></title>

    <script src="http://rawgithub.com/timseverien/cm.js/master/cm.js"></script>
</head>
<body>

<div id="time"></div>

<script type="text/javascript">

(function(){
    var Timer = window.Timer = function(t, el, cookie){
        this.cookie = cookie || false;
        this.el = el;
        this.t = this.cookie&&CM.get('t') || (t * 60 * 1000);
        this.d = new Date(this.t);
        this.event = new CustomEvent('onend');

        this.show();

        return this.el;
    }

    //https://gist.github.com/aemkei/1180489
    Timer.prototype.pad = function(a,b){return([1e15]+a).slice(-b)};
    Timer.prototype.format = function(){
        return this.pad(this.d.getMinutes(), 2) + ':' + this.pad(this.d.getSeconds(), 2);
    }
    Timer.prototype.show = function(){
        var self = this;


        window.addEventListener('beforeunload', function(event) {
            self.cookie&&CM.set('t', self.t);
        }, false);

        self.el.innerHTML = self.format();

        setTimeout(function(){
            self.t -= 1000;
            self.d = new Date(self.t);

            if(self.t > 0) 
                self.show();
            else{
                CM.clear();
                self.el.innerHTML = self.format();
                self.el.dispatchEvent(self.event);
            }
        }, 1000)
    }
})();

var timer = new Timer(5, document.getElementById('time'), true);
timer.addEventListener('onend', function(e){
    alert('timer is ended! (you can refresh here)');
    //location.reload();
}, false)
</script>
</body>
</html>

here is live example (without the cookie) http://jsfiddle.net/55p7f/ 这是现场示例(没有cookie) http://jsfiddle.net/55p7f/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM