Last updated on May 18th, 2016 at 03:12 pm

Auto refresh page using Javascript

Actually this javascript program have been there long since javascript is made. But getting a page refreshed within a specific time frame is a great thing to get added to your website. In this script we can implement a timer which displays a countdown before the web page gets refreshed.

Code explanation:- For the code below I am setting time limit as 30 seconds. parselimit variable will have the time in seconds. For example if you are setting limit as one minute and thirty seconds (1:30), then this will be converted to seconds by the variable parselimit.

The main code will come under function named beginrefresh from where it basically checks whether variable parselimit is equal to one then reload the web page otherwise keep decrementing the time.

Countdown will be shown in the div with id=”status”

<script>
//enter refresh time in "minutes:seconds" Minutes should range from 0 to inifinity. / / enter refresh time in "minutes: seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59 Seconds should range from 0 to 59
var limit="0:30" 
parselimit=limit.split(":") 
parselimit = parselimit [0] * 60 + parselimit [1] * 1
function beginrefresh(){
if (parselimit==1) 
window.location.reload() 
else{
parselimit-=1 
curmin=Math.floor(parselimit/60) 
cursec=parselimit%60 
if (curmin!=0) 
curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!"
else 
curtime=cursec+" seconds left until page refresh!"
document.getElementById("status").innerHTML=curtime 
setTimeout("beginrefresh()",1000)
} 
}
window.onload=beginrefresh 

</script> 
<div id="status"></div>

You can use this web page to call some Database page or something similar that needs to be refreshed after a specific time period.

Leave a Reply

Your email address will not be published. Required fields are marked *