簡體   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