0

I wanted the page to refresh after 1 second and that's it, but after 1 second I get an infinite page refresh. How to fix endless page refresh?

function sayHi() {
  document.location.reload();
  return false;
}
setTimeout(sayHi, 1000);
<div class="timer">
  Infinity refresh =(
</div>
Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
alderman
  • 69
  • 5
  • 2
    This code looks intentionally designed to infinitely refresh the page. What are you trying to accomplish with this code? What is you ruse case to refresh the page 1 second after it loads? – mhodges Mar 30 '22 at 20:09
  • 2
    Refreshing the page is causing the timeout to be set again, because the script is reloading too. – TheKodeToad Mar 30 '22 at 20:09
  • You probably want to set a cookie etc. to see if the user has already visited the page and then not execute that code again. Also, what's the point of having the page refresh once? – j08691 Mar 30 '22 at 20:09
  • I want the page to refresh only 1 time I recently started learning js and don't quite understand it – alderman Mar 30 '22 at 20:10
  • This is most likely because each time you refresh the page your code is all run again, a brand new one second timeout is set, and it refreshes again, which re-runs the code, which sets a new one second timeout, which refreshes the page again, etc., etc. Your JS will completely refresh with a page refresh and anything in JS memory will be lost. To only do it a single time you'd need some external source of state, such as [`sessionStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage). Good luck, and happy coding! – Alexander Nied Mar 30 '22 at 20:11
  • 2
    @alderman This seems like an [XY Problem](https://xyproblem.info/) Why are you needing to refresh the page right after it loads? What problem are you trying to solve? Maybe we can better help with that instead – mhodges Mar 30 '22 at 20:11
  • You're going to have to use local storage or something. Set the page refresh counts in local storage and check them in your `sayHi` function. – Joshua Wood Mar 30 '22 at 20:11
  • 1
    Please revise your post title to ask a clear, specific question. See [ask]. – isherwood Mar 30 '22 at 20:11
  • Don't see why this isn't a clear, specific question. – TheKodeToad Mar 30 '22 at 20:13
  • 2
    I agree with @mhodges - unless this is a purely theoretical exercise, I am having a hard time envisioning what use case this code might be for. It would be worth sharing context that explains _why_ you are doing this. – Alexander Nied Mar 30 '22 at 20:14
  • The other similar post had 93 upvotes. Dunno why this one was downvoted. Of course, there is usually a better solution. – TheKodeToad Mar 30 '22 at 20:15
  • 2
    @MrParrot 2 reasons, either A: it's what they were wanting to ask, and the fact that a post with 93 upvotes asking the exact same question already exists so we downvote to show a "lack of research effort" or B: It's not what they're actually trying to ask about, in which case the question is "unclear" - both perfectly valid reasons for downvoting a question – mhodges Mar 30 '22 at 20:17
  • It is clear, although I must admit the other question came up on the first search result. Don't see how it's directly a problem with the question itself. Honestly, I've never really got the idea of upvoting and downvoting questions. I probably shouldn't be using this website! – TheKodeToad Mar 31 '22 at 08:30
  • 1
    @MrParrot, "Endless page refresh" isn't a question to begin with, and it certainly isn't clear and specific. (I did say _title_.) See [ask] (also linked above for your earlier convenience) for some insight into site standards. – isherwood Mar 31 '22 at 14:13

1 Answers1

2

When you reload the page, it's going to run the setTimeout again. You can use sessionStorage to make sure you refresh once per session.

if(!sessionStorage.getItem('refreshed')) {
    sessionStorage.setItem('refreshed', true);
    setTimeout(sayHi, 1000);
}
Mystical
  • 2,505
  • 2
  • 24
  • 43