0
window.onload = function() {
        onPageLoad();
}
var reftime;
function refreshPage(){
        var refreshtime = document.getElementById("r_time").value;
        //var reftime;
        reftime = setInterval(function() {
                window.location.reload(true);
        }, refreshtime*60000);
}

Above code is Js for reloading page.

<input id='r_time' type='text' name='r_time' value= ''/>
           
<input id='r_btn' type='button' value='Apply' style="font-size:12px;font-family:Helvetica;" onclick='refreshPage();'/>

This is my code it for reload my page it is reloading once when i gave time interval in minutes.But after once reloaded it is not reloading again.

  • after page is reloaded the event is set to null and you have to click the button again in order to assign event handler to the event – Peter Isaac Oct 22 '20 at 11:15

5 Answers5

0

The problem is that after reloading the window with window.location.reload(true) all your javascipt variables and intervals are gone. So as soon as you reloaded the page for the first time no interval is running that will reload the page another time.

To get around this issue you have a few possibilities:

  1. Don't reload the whole page. Just reload part of the page that changes. This could be done with an asynchronous AJAX call retrieving the content to display from a server
  2. Check out this solution to learn how to keep variables after reloading a html page. After the reload you can then check in the window.onload function if you should start another inverval after some specified time.
Kylro
  • 161
  • 4
0

For exaple if you want your page to reload itself after 1 minute, You can try just writing location.reload(0) in your onPageLoad() function like this:

window.onload = function() {
  setTimeout(onPageLoad, 60000);
}

function onPageLoad() { 
   location.reload(0);
}

That should make your page reload after 1 minute(example).

David Ngumbu
  • 134
  • 9
  • yes but i want to reload at given time util user started using any thing on page. The page should reload by user given time –  Oct 22 '20 at 11:29
  • Trying to get it. Do you want the user to be the one to set the time for the page to reload. – David Ngumbu Oct 22 '20 at 19:14
0

Upon refresh, you'll start over again and the values of variables set will be gone. You can save the value using localStorage. Also, using setInterval in this case is unnecessary, you must use setTimeout

var reftime;
var input = document.getElementById("r_time");

window.onload = function() {
    reftime = localStorage.getItem('time');
    
  if (reftime) {
    input.value = reftime;
    refreshPage();
  }
}

function refreshPage(){
        var time = input.value || reftime;
  
  localStorage.setItem('time', time);
        setTimeout(function() {
                window.location.reload(true);
        }, time);
}
Julius Guevarra
  • 690
  • 1
  • 7
  • 19
  • may i know why 'time' –  Oct 22 '20 at 11:56
  • In thi it is not getting value in var input . –  Oct 22 '20 at 12:13
  • local variable ```time``` for the refreshPage and the value set to it is either the input value or the time which was already saved localStorage. If you want to set time to minutes, you can put back the old time argument ```setTimeout(function() { ...}, time * 60000)``` – Julius Guevarra Oct 22 '20 at 12:20
  • When i try to print alert(time ); it showing as undefined –  Oct 22 '20 at 12:44
  • where did you put the alert call? It is undefined when you first run it. But when you enter an input, it shouldn't be. – Julius Guevarra Oct 22 '20 at 12:53
  • hey please help me it is not working when my restart or reboot. –  Oct 27 '20 at 04:43
  • If you clear the browser storage or run it in incognito, it won't work again after you restarted/rebooted. – Julius Guevarra Oct 27 '20 at 10:10
  • can you help how to write into file and use it –  Oct 27 '20 at 10:36
0
window.onload = function () {
    onPageLoad();
}
var reftime;

function refreshPage() {
    var refreshtime = document.getElementById("r_time").value;
    // var reftime;
    reftime = setInterval(function () {
        location.reload(true);
    }, refreshtime * 60000);
}

See if that Works.

0

For this code I am using cookies to save the value every time the website refreshes.

const _ = {
    cookie_class: class cookies {
        constructor() {

        }
        setCookie(cookie_name, cookie_value, expire_in_days) {
            var d = new Date();
            d.setTime(d.getTime() + (expire_in_days * 24 * 60 * 60 * 1000));
            var expires = "expires=" + d.toUTCString();
            document.cookie = cookie_name + "=" + cookie_value + ";" + expires + ";path=/";
        }

        getCookie(cookie_name) {
            var name = cookie_name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') {
                    c = c.substring(1);
                }
                if (c.indexOf(name) == 0) {
                    return c.substring(name.length, c.length);
                }
            }
            return "";
        }
        exists(cookie) {
            var user = this.getCookie(cookie);
            if (user != "") {
                return true;
            } else {
                return false;
            }
        }
    }
}
const cookies = new _.cookie_class();
let interval;
const refresh = () => {
    location.reload();
}
let cookie_name = "refresh_interval";
window.onload = () => {
    if (cookies.exists(cookie_name)) {
        interval = setInterval(refresh, parseInt(cookies.getCookie(cookie_name)));
    }
}

const update = () => {
    let value = document.getElementById('input').value;
    if (!isNaN(parseInt(value))) {
        cookies.setCookie(cookie_name, parseInt(value), 365);
        clearInterval(interval);
        interval = setInterval(refresh, parseInt(value));
    } else {
        // Handle is however you would Like:
        // The input is not a Number
    }
};

// Listeners

document.getElementById('apply-btn').addEventListener('click', (event) => {
    event.preventDefault();
    update();
});

Tell me if it works for you!