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';
9
  • you can use location.reload(); to refresh the page on Ajax success. Commented Dec 27, 2018 at 7:27
  • Is the above code on page1.php? Commented Dec 27, 2018 at 7:27
  • @JackBashford no that is currentpage.php Commented Dec 27, 2018 at 7:29
  • @onkar.v no, that is not what i meant, please read my problem above again sir, i want to refresh specific pages not the same page Commented Dec 27, 2018 at 7:30
  • window.location.href = "page1.php"; Commented Dec 27, 2018 at 7:35

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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 :(
i mean let's just hide that refresh but tho even if it is hide the page will still refresh
The page should just refresh once after 3 seconds and then not refresh again. Is this not working?
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");

1 Comment

you cant use header in javascript, its php function
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.