-2

some friends help me to write the following code to reload page once time after 1 sec but it had syntax error where i did not know.can any one help me to edit?

window.addEventListener('load', () => {
if(!window.localStorage.getItem("reload")) setTimeout(() => {
    window.localStorage.setItem("reload", "true");
    window.location.reload();
}, 1000)
});
window.addEventListener('unload',()=>{
window.localStorage.removeItem("reload");
})

Thanks

3
  • 3
    You'll have to describe what you've tried otherwise the answers will probably look like the answers to the other questions which you said did not help you, at best. Commented Jun 16, 2021 at 12:43
  • window.location.reload() doesn't change the hash Commented Jun 16, 2021 at 12:43
  • Reload the page with window.location.reload and set your timeout with setTimeout Commented Jun 16, 2021 at 12:44

1 Answer 1

0
window.addEventListener('load', () => {
    if(!window.sessionStorage.getItem("reload")) setTimeout(() => {
        window.sessionStorage.setItem("reload", "true");
        window.location.reload();
    }, 1000)
});
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your help.I use your code but the page refresh many times after 1 sec and don't stop. I need it refresh once time.
yeah, my bad, I didn't notice that, I edited the code for you.
thanks for your comment.is this code edited? because i have test it and it is same as before reload many times
I'll edit the answer again, using sessionStorage instead of localStorage

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.