Friday, September 18, 2009

Refreshing page after some specific interval

If you have requirement such as you want to refresh your page after every few seconds or minutes, you are reading right blog. I found it very easy to implement. Please go through below example and you will get the exact idea.

Server side code

protected void Page_Load(object sender, EventArgs e)
{
Response.AddHeader("refresh", "5");
}

Client side code

<html>
<head>

<script language="javascript" type="text/javascript">
function fn()
{
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var seconds = currentTime.getSeconds()
if (minutes < 10){
minutes = "0" + minutes
}
if (seconds < 10){
seconds = "0" + seconds
}
document.getElementById("div1").innerHTML = hours + ":" + minutes + ":" + seconds
}
</script>

</head>
<body onload="fn();">
<div>
<div id="div1" />
</div>
</body>
</html>

In the above example I have implemented client side watch, which refreshes every five seconds. The only line of code required to write to submit the page is Response.AddHeader("refresh", "5");, other is just custom logic.

happy coding... :)

No comments:

Post a Comment