How to delay a page refresh in javascript? - javascript

I want to refresh a web page using javascript but when any event occur after few second the page will refresh.
what can i do?

You can try setTimeout()
setTimeout(function() {
// Do something after 5 seconds
location.reload();//reload page
}, 5000);

Use meta tag like <meta http-equiv="Refresh" content="300">
This wiil make your page to refresh every 5minutes (5*60)

Related

Firefox prevoius page button reload setInterval

I have a link to a page. The loading is slow and on click event i show a progress bar and run a timing loop with random text. I do this with setInterval. I change the text every 8 seconds.
When the loading is completed, the browser shows the new page.
I don't clear Interval because i think the new page doesn't share anything with the previous page.
But, if i click on the previous button on Firefox, it seems to remember the there was an intervarl and it runs it again.
In Chrome, the prevoius button seems to reload the page and the interval there isn't.
Why Firefox has this behavoiur?
You can see the page here:
http://www.demo.tinnservice.com:8090/
I set the interval on click to "Ricerca Avanzata"
This is the html tag
<a class="linkSlow" data-action="archivio" href="/archivio">Ricerca avanzata</a>
This is the javascript code:
function smokeInTheEyes(element){
$("body").css("position","relative");
$("#wait-overlay").addClass("in");
var frasi=frasiObj[element.data("action")];
var i=0;
setTimeout(function(){$("#frase").text(frasi[i]).show("400");},1000)
var timer=setInterval(function(){
i+=1;
console.log(i);
if(i==frasi.length){i=0;}
$("#frase").hide("400",function(){$("#frase").text(frasi[i]).show("400")});
}, 8000);
}
$(".linkSlow").click(function(e){
smokeInTheEyes($(this));
});
PS: Frasi is an object with two or more array of strings. The data-action attribute on the clicked tag tell me which array i use to display the random text
Possible ducplicate:
Force Firefox to Reload Page on Back Button
This is the accepted answer created by jknair:
add this between your HEAD tags
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">

Is there a better way than meta refresh when connection is lost?

I use often <meta http-equiv="refresh" content="60"> to update my page.
I noticed that this stops, if for any reason the connection to the server get lost for a while.
My first thought was to add a PHP information, when the page will be updated the next time, and a JavaScript variable what time it is now. This would require an action from the user. Can I do that differently?
You can use setTimeout
window.addEventListener('load', reloadPage, false);
function reloadPage() {
// reload the page in 60 seconds
setTimeout(function(){ location.reload(); }, 60000);
}

Refreshing a page after certain delay in jquery [duplicate]

This question already has answers here:
How to schedule IE page reload
(4 answers)
Closed 8 years ago.
here's my problem: i need to display a message for a while, and then reload the page.
can someone tell me how to reload a page, after certain delay?
You don't even need jQuery or HTML5 for this:
setTimeout(location.reload.bind(location), 60000);
This will wait 1 minute (60,000 milliseconds), then call the location.reload function, which is a built-in function to refresh the page.
setTimeout(function(){
window.location.reload(); // you can pass true to reload function to ignore the client cache and reload from the server
},delayTime); //delayTime should be written in milliseconds e.g. 1000 which equals 1 second
Update:
One-liner using ES6:
setTimeout(() => window.location.reload(), delayTime);
You may try this without js, it cycles:
<meta http-equiv="refresh" content="5"/> <!-- 5 sec interval-->
<h1>Page refersh in every 5 seconds...</h1>
You can even navigate to a different page, visiting google home page
<meta http-equiv="refresh" content="5;http://www.google.com"/> <!-- 5 sec delay-->
<h1>Redirecting in 5 seconds...</h1>

How redirect to homepage with php

I need do a automatic foward/redirect.
If the user dont click anywhere in the site five seconds after automatic foward to homepage... it is possible?
For example,
<meta http-equiv="Refresh" content="5;URL=http://www.teste.com/sv1/index.html">
Thanks
try this in your head:
<script type="text/javascript">
var redirect = setTimeout(function() {
window.location = "http://stackoverflow.com";
}, 5000);
document.onclick = function() {
clearTimeout(redirect);
}
</script>
In PHP it's not possible. You can add timeout in JS and stop it when user click somewhere (onclick event on body).
After that timeout (without clicks), yuo can redirect user by setting document.location.href to your homepage.
Short: No.
Longer: This is not possible with PHP, because PHP is precompiled on the server. So as soon as the user sees the page on his browser, the PHP script already ran through. You will have to use something else instead, for example JavaScript.

Reload a url in each seconds with jquery

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?
setInterval(window.location = $('#thisLink').attr('href'), 1000);
DEMO: http://jsfiddle.net/QBMLm/
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.
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
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', '');
});​

Categories

Resources