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);
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);
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?
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.
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.
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', '');
});
Look that these which may help you:
JS setInterval executes only once setInterval with jQuery.html only updates once? http://www.google.com/search?q=jquery+setinterval+only+running+once&aq=0&oq=jquery+setinterval+only+running+once&sugexp=chrome,mod=1&sourceid=chrome&ie=UTF-8