Updating page after time

Asked

Viewed 11,972 times

3

I made a page for real-time data sample, and need real-time update after 3 minutes.

I tried that code:

$(function() {
    setTimeout(function(){ location.reload(); }, 180000);
});

Unproven.

I don’t know if I need to use the (document) to perform the verification in the certainty of the entire loaded document, but within the ready I thought it would solve.

3 answers

7


You can do that only with HTML:

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

Where content is the waiting time (in seconds) until refresh

With javascript (you don’t even need jquery), try putting window:

setTimeout(function() {
  window.location.reload(1);
}, 180000); // 3 minutos
  • The Meta part could not use, by the structure of my system (Goals are elsewhere, and time is for only one part). But Javascript worked. Missing window.

  • Ha ok, but know that you can use meta another time when you think applicable. Yeah, the window was missing and I didn’t even notice that lapse :P . I’ll put that addition in the answer

  • This story of missing window. is wrong, window is a global object, accessible to all scopes and it is not necessary to type, what must have occurred is that the author created some variable var location = xyz; and this affected the current scope, minus the global.

  • 1

    @Guilhermenascimento is true. Well noted

  • You have been mentioned here: http://meta.pt.stackoverflow.com/q/5719/132 - I invite you to express yourself.

  • Thank you @Victorstafusa, I will

Show 1 more comment

2

Can do with Jquery also:

$(document).ready(function () {
    setTimeout(function () {
        window.location.reload(1);
    }, 5000); //tempo em milisegundos. Neste caso, o refresh vai acontecer de 5 em 5 segundos.
});

Remembering that for this to work, you need to have jquery included before the script declaration.

2

You tried to wait 10 minutes?

600000 milliseconds = 10 minutes.

$(function() {
    setTimeout(function(){ location.reload(); }, 600000); // 10 * 60 * 1000
});

If you want 3 minutes, try 180000 milliseconds.

$(function() {
    setTimeout(function(){ location.reload(); }, 180000); // 3 * 60 * 1000
});

In the example below you can see the same code being used after 3 seconds:

$(function() {
  setTimeout(function(){ 
    $("body").append(" o timeout aconteceu.");
  }, 3000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
Três segundos depois...
</body>

  • I ended up showing from another place that also got the timeout that wasn’t working

  • @Williamasimiliar the timeout in itself is working; I have included a functional example for testing. Your problem may be another.

  • 2

    @Williamaparecidobrandino Repeating, only to have emphasis: This story of missing window. is wrong, window is a global object, accessible to all scopes and it is not necessary to type, what must have occurred is that the author created some variable var location = xyz; and this has affected the current scope, less the global one. Or you are making a mess.

  • @Guilhermenascimento As a legacy system (more than 20 years), you must have lost something using reserved words. I don’t doubt that’s it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.