-5

I do not know where to put the function

setTimeout('history.go(0);', 10000)  

how do I code on a page?

JoSSte
  • 2,953
  • 6
  • 34
  • 54
Elisa
  • 1
  • 4
    Question is....why do you need to do that ? :) There SHOULD be a better solution to your problem than to refresh the page every 10 seconds. – Mihai T Nov 26 '22 at 11:31
  • Welcome to Stack overflow. Please ensure that you have included all the information you can think of that will help another person read, understand and provide an answer to your question. Less information will leave us guessing, and not give you a answer. Try to read through your question and ask yourself "If I didn't know anything about what I am doing, is this enough to answer this question?" - if you answer no, provide more detail. Read [ask] as well to give you some tips. Help others to help you – JoSSte Nov 26 '22 at 11:47
  • Your code works. What's your question? – Donald Duck Nov 26 '22 at 14:59
  • _“how do I code on a page?”_ — If your question is _“How do I write JavaScript code?”_, what prevents you from just looking it up? The [ES](//tc39.es/ecma262), [HTML](//html.spec.whatwg.org), and [DOM](//dom.spec.whatwg.org) spec, [JS](//developer.mozilla.org/en/docs/Web/JavaScript/Reference) and [HTML](//developer.mozilla.org/en/docs/Web/HTML/Reference) docs, [JS](//developer.mozilla.org/en/docs/Web/JavaScript/Guide) and [HTML](//developer.mozilla.org/en/docs/Learn/HTML) tutorials, and [inspectors](//developer.mozilla.org/en/docs/Learn/HTML/Introduction_to_HTML/Debugging_HTML) are all there. – Sebastian Simon Nov 26 '22 at 15:10

3 Answers3

1

One place you could put it is here:

<html>
  <head>
    <script>
      setTimeout('history.go(0);', 10000)  
    </script>
  <head>
  <body>
   ..
  </body>
</html>

another, better way is to write the script as follows

<html>

<head>
    <script>
        //console.info('initial load');
        addEventListener('DOMContentLoaded', function () {
            setTimeout(function () {
                //console.info('reloading');
                window.location.reload();
            }, 10000);
        })
    </script>

    <head>

    <body>
        my body
    </body>

</html>

because this will wait for the whole document to be loaded before executing.

I would recommend reading up on AJAX and only fetching the parts you need as an asynchronous event, instead of loading the whole page again and again...

JoSSte
  • 2,953
  • 6
  • 34
  • 54
  • 1
    You should use an `addEventListener('DOMContentLoaded', function() { ... })` instead of using "outdated" `onload` function. Alternativly just add a defer attribute or move it the the bottom fo the body. – tacoshy Nov 26 '22 at 11:11
  • I agree @tacoshy, and as I commented, I would go for an asyncronous reload of the specific content, not for a complete page reload - If the original page is loaded with POST you'd get browser dialogs and such to deal with too. – JoSSte Nov 26 '22 at 11:33
  • @tacoshy since when is `onload` ' outdated' ? Can you share a spec saying that ? I honestly couldn't find any. And also I guess the OP is using React or a js framework ( use of `history` object and not `window` ) – Mihai T Nov 26 '22 at 11:34
  • 1
    `onload` overwrites any other onload events. `addEventListener('DOMContentLoaded',...)` appends an extra action to the onload event. that is what he meant by outdated. Arguments can be made either way besides that. The onload approach makes it potentially simpler to read the code, the addEventListener allows you to collect all your javascript in .js files. even this post from 2014 indicates onload as legacy code https://stackoverflow.com/a/1236044/1725871 – JoSSte Nov 26 '22 at 11:41
  • @MihaiT how did you conclude that since there are no tags inicating it? just the `history.go(0)` ??? – JoSSte Nov 26 '22 at 11:43
  • 1
    @JoSSte It's just a guess. But also ` 'history.go(0);'` should be without quotes. Otherwise it's just a string. – Mihai T Nov 26 '22 at 11:44
  • 1
    @MihaiT _“since when is `onload` outdated?”_ — There are [better alternatives](/q/14028959/4642212) to a `load` listener, like `type="module"`, `defer`, or `DOMContentLoaded`. Which of the four to use depends on use case; none of these are deprecated, so I don’t know what you hope to find in the spec. _“`'history.go(0);'` should be without quotes. Otherwise it’s just a string.”_ — Which is what [`setTimeout`](//developer.mozilla.org/en/docs/Web/API/setTimeout) allows. If not a string, then it must be a function. `history.go(0)` is not a function, `history.go(0);` isn’t even an expression. – Sebastian Simon Nov 26 '22 at 14:58
  • 1
    @JoSSte Is right, You probably don't need the reload the whole page! + 1 – tatactic Nov 26 '22 at 17:57
1

You don't even need a script... Just add

<meta http-equiv="refresh" content="10">

in the headers.

Why do you try to do this via JS???

tatactic
  • 1,379
  • 1
  • 9
  • 18
0
  1. You need a timeout function to execute after 10 seconds (10000ms)
  2. You can reload the page with window.location.reload()
setTimeout(function () {
  window.location.reload();
}, 10000);
tacoshy
  • 10,642
  • 5
  • 17
  • 34