0

How can i refresh this page one time only not repeating

    function timedRefresh(timeoutPeriod) {
  setTimeout("location.reload(false);",timeoutPeriod);
}

window.onload = timedRefresh(3000);

document.getElementById('myModal').style.display='block';

4 Answers4

1
int refresh=0;  

function timedRefresh(timeoutPeriod) {
  setTimeout("location.reload(false);",timeoutPeriod);
  refresh=1;
}

if(refresh==0){
window.onload = timedRefresh(3000);
}


document.getElementById('myModal').style.display='block';

you can add a flag boolean or integer to check if page is loaded or not

0

you can refresh the page by using PHP

header('Refresh: 0; url=page1.php');

To refresh the current page, you can use -

header("Refresh:0");
onkar.v
  • 135
  • 9
0

Although Zeljka's linked answer is future proof, if you already use hashes for navigation it might better to use performance.navigation.type.

window.onload = function() {
    if(performance.navigation.type != 1) {
       timedRefresh(3000);
    }
}

function timedRefresh(time) {
    setTimeout(function() {
        location.reload()
    }, time);
}

Note that the NavigationType is depreciated, but looks like the new spec will have an equivalent when it arrives.

0

The problem is that you reload the exact same page so with no way to persist the fact that you already ran that code it will just run it again. The old-fashioned way of doing this was using cookies:

function timedRefreshIfNoCookie(timeoutPeriod, cookieName) {
    var cookies = document.cookie;
    if (cookies.indexOf(' '+cookieName+'=1;') < 0) {
        document.cookie = cookieName+'=1';
        setTimeout(function () {
            location.reload();
        }, timeoutPeriod);
    }
}
window.onload = function() {
    timedRefreshIfNoCookie(3000, 'refreshed');
}    
document.getElementById('myModal').style.display='block';

You can probably use something a bit better like local storage or a cookie library to work with cookies more easily.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • hey sir thanks it worked ! but one last problem is why the page is refreshin every like 3 seconds? can we just remove that refresh because it refresh everytime my pc lags :( –  Dec 28 '18 at 04:54
  • i mean let's just hide that refresh but tho even if it is hide the page will still refresh –  Dec 28 '18 at 04:55
  • The page should just refresh once after 3 seconds and then not refresh again. Is this not working? – apokryfos Dec 28 '18 at 09:16