Refreshing a page after certain delay in jquery [duplicate] - javascript

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>

Related

How to write a time delay which doesn't freeze the UI of the website? [duplicate]

This question already has answers here:
How to set time delay in javascript
(10 answers)
Closed 4 years ago.
I want to make a time delay in my website. So far I have built a sleep function which can freeze my system for number of seconds. But with this, I'm not able to my access the User Interface of the website.
My Code:
<html>
<head>
<title></title>
</head>
<body>
<button onclick="on_click()">Button-1</button>
<button onclick="console.log('BYE');">Button-2</button>
<script>
function on_click()
{
sleep(15000);
console.log("HI");
}
function sleep(delay)
{
var start = new Date().getTime();
while (new Date().getTime() < start + delay);
}
</script>
</body>
</html>
So whenever I click Button-1, I'll have to wait for the 15 seconds to click the Button-2. I want my program to allow me to click on Button-1 and it should wait for 15 seconds and display "Hi" on the console. And also allowing me to click Button-2. Please Help. Thanks
The setTimeout function allows you to set a time delay without stopping the script from running ongoing processes.

popup once every 5 minute [duplicate]

This question already has answers here:
jquery/javascript alert popup on a set time
(2 answers)
Closed 5 years ago.
i have a bootstrap modal popup with following code that runs every time page loads.
I need the script run once for each session with cookie and cookie will expire after 5 minutes. can you please help?
here's the code:
$(window).on('load',function(){
$('#myModal').modal('show');
});
setInterval(showModal, 300000);
function showModal()
{
$('#myModal').modal('show');
}

How to delay a page refresh in 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)

refresh the previous page with history.back [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
History.Back with refresh
I have an input button that I have assigned with onclick="history.back()" .
Is there a way to use this but force the previous page to be refreshed?
I want it to work in FF and IE8
Depending on what exactly you need, here is a fix which needs some more preparation at first, but should work:
Place an invisible element with a (serverside created) timestamp in your document.
Place an eventlistenener which checks the timestamp and reloads the page if it is too old.
Now when you navigate back to this site, either via your button or the browser's back button, the onload handler will fire, check the date and reload the page if necessary.
client side pseudo code:
document.body.addEventListener("load",function(){
// compare timestamp stored in element with current time and if it is too old,
// reload page e.g. via document.location.reload()
var d1 = document.querySelector("#timestamp").innerHTML;
var d2 = Date.now;
if (d2-d1 > 10000 ) document.location.reload();
});

Refresh Page for interval using js

How can i refresh a page for every one minute using javascript.
Note: I don't have control/option to edit HTML body tag (where we usually call onload function).
Just insert this code anywhere in the page:
<script type="text/javascript">
setTimeout(function(){
location = ''
},60000)
</script>
<script type="text/javascript">
setTimeout(function () {
location.reload();
}, 60 * 1000);
</script>
setTimeout will reload the page after a specified number of milliseconds, hence 60 * 1000 = 1m. Also, since the page is being refreshed, the timeout will always be set on page load.
You do not need to have the code in the body tag. Just add this snippet below and it should work no matter where it is in the page.
<script type="text/javascript">
setInterval('window.location.reload()', 60000);
</script>
As long as you can access the HTML some where and your editor doesn't filter out tags you should be fine. If your editor has a separate area for JavaScript code then just enter setInterval line. :)
Here's the thing mate!
(Point 4 is for this particular question)
1). If you want to reload the same windows over and over again then just execute
window.location.reload()
2). If you want to hard reload from the server then execute
window.location.reload(true)
(basically, just pass true as a boolean arg to the same line of code)
3). If you want to do the same job as point 1 and 2 with a time out. i.e. execute the reload after some time JUST ONCE, then execute
setTimeout("window.location.reload()",10000);
(this should execute on the window after 10 sec. JUST ONCE)
4). If you want to keep reloading the window with a certain timeout then execute
setInterval("window.location.reload()",10000);
(this should execute on the window after 10 sec. with 10 sec. for the interval)
Surely,there're many ways to pass a callback..
setInterval(function(){window.location.reload();},10000);
or
<code>
function call1(){
location.reload(true);
}
setInterval(call1,10000);
</code>
Note:
-Have a look at MDN Guides for [setTimeout][1] and [setInterval][2] functions.
-Using the window object is optional but good to be used. (window is a global object and already available to your current window.)
If you don't want to edit the page, here's the trick. Open the console and write the below-mentioned snippet.
INTERVAL = 5 // seconds
STOP_AFTER = 15 // seconds
// Open the same link in the new tab
win1 = window.open(location.href);
// At every 5 seconds, reload the page
timer1 = setInterval(() => {
win1.location.reload();
console.log("Refreshed");
},INTERVAL*1000)
// Stop reloading after 15 seconds
setTimeout(() => clearInterval(timer1), STOP_AFTER*1000)
Since you want to reload it, you can not simply write location.reload() since the console will be cleared once it is reloaded.
Therefore, it will open a new tab with the same link. It will be easily able to control the 2nd tab using the console of the 1st tab.
When your URL has parameters, it seems that using location = '' doesn't work in IE8. The page reloads without any parameters.
The following code works for me :
<script type="text/javascript">
setTimeout(function(){
window.location.href = window.location.href;
},10000)
</script>

Categories

Resources