繁体   English   中英

javascript自动加载无限循环,剩余时间直到下次重新加载

[英]javascript autoreload in infinite loop with time left till next reload

我需要一个JavaScript,它每30秒对一个页面进行一次重新下载,并显示在下次重新加载ID时需要多长时间,例如:

<p>Refreshing in <span id="time-to-update" class="light-blue"></span> seconds.</p>

我也需要它无限地重复自己。

谢谢你的阅读,我希望它不会帮助我,除了其他所有人,如果你能制作这个剧本,真的非常感谢你。

(function() {
    var el = document.getElementById('time-to-update');
    var count = 30;
    setInterval(function() {
        count -= 1;
        el.innerHTML = count;
        if (count == 0) {
            location.reload();
        }
    }, 1000);
})();

使用setTimeout而不是setInterval的变体,并使用更多跨浏览器的安全 document.location.reload(true);

var timer = 30;
var el = document.getElementById('time-to-update');

(function loop(el) {
  if (timer > 0) {
    el.innerHTML = timer;
    timer -= 1;
    setTimeout(function () { loop(el); }, 1000);
  } else {
    document.location.reload(true);
  }
}(el));

http://jsfiddle.net/zGGEH/1/

var timer = {
    interval: null,
    seconds: 30,

    start: function () {
        var self = this,
            el = document.getElementById('time-to-update');

        el.innerText = this.seconds; // Output initial value

        this.interval = setInterval(function () {
            self.seconds--;

            if (self.seconds == 0) 
                window.location.reload();

            el.innerText = self.seconds;
        }, 1000);
    },

    stop: function () {
        window.clearInterval(this.interval)
    }
}

timer.start();

暂无
暂无

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

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