1

I want reload a url in each seconds with jquery, i try as following code, this code reloading url only once. How do i do?

<a href="http://myweb.com/" id="thisLink"></a>

setInterval(window.location = $('#thisLink').attr('href'), 1000);

DEMO: http://jsfiddle.net/QBMLm/

jennifer Jolie
  • 727
  • 6
  • 16
  • 30
  • 1
    This is really annoying, but there actually is a meta refresh tag that does this without any javascript at all. – adeneo Oct 03 '12 at 13:17

5 Answers5

1

If it's your page, you can use this in the head :

<meta http-equiv="refresh" content="1;url=/">

of course, this only works for the page it's embedded in, and won't keep reloading some other external site?

adeneo
  • 312,895
  • 29
  • 395
  • 388
1

setInterval is not persistent between browser reloads. Also, it takes a function as first argument. You can try something like:

setTimeout(function() {
    window.location = $('#thisLink').attr('href');
}, 1000);

It will wait 1sec before redirecting. If the page you are redirecting to have the same code, it will do the same.

David Hellsing
  • 106,495
  • 44
  • 176
  • 212
0

Once you re-load the url (window.location change) the context (and scope) of that setInterval become moot (the page is discarded and the next is loaded). The script's then reloaded and setInterval reassigned.

Oh, and syntactically that code is invalid. You probably want to wrap the window.location portion in a function(){}, e.g.

setInterval(function(){
  window.location = $('#thisLink').attr('href')
}, 1000);

otherwise it's not actually executing in an interval fashion, but immediately.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • @jenniferJolie: That's what i explained in the first half of my aswer. You can't use `setInterval` and `window.location` in the same statement (but did show how it _should_ look while retaining the code). – Brad Christie Oct 03 '12 at 13:27
0

It's reloading only once, since once you change window.location you leave your page.

You need to open the link in new named window or embed the child page in an iframe.

setInterval(function() {
    window.open($('#thisLink').attr('href'), 'mywindow', '');
});​
Michal Klouda
  • 14,263
  • 7
  • 53
  • 77