最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - javascript autoreload in infinite loop with time left till next reload - Stack Overflow

programmeradmin2浏览0评论

i need a JavaScript, that relaods a page every 30 seconds, and will show how much time there is until next reload at the ID time-to-update, Example:

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

i also need it to repeat itself infinitely.

thank you for reading, i hope it helps not me but everyone else, and a real big thank you if you could make this script.

i need a JavaScript, that relaods a page every 30 seconds, and will show how much time there is until next reload at the ID time-to-update, Example:

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

i also need it to repeat itself infinitely.

thank you for reading, i hope it helps not me but everyone else, and a real big thank you if you could make this script.

Share Improve this question edited Aug 19, 2013 at 11:42 Srinivas 1,0638 silver badges15 bronze badges asked Aug 19, 2013 at 11:33 Mister_FixMister_Fix 732 silver badges11 bronze badges 0
Add a ment  | 

3 Answers 3

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

A variation that uses setTimeout rather than setInterval, and uses the more cross-browser secure 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/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();
发布评论

评论列表(0)

  1. 暂无评论