I know there should be an easy way to do this but I guess my brain isn’t working today. I need to refresh a page in x number of seconds. I can do that in .NET using the following:
Response.AddHeader('Refresh', '300');
This is fine except I also need to display the number of minutes and seconds until the next refresh.
So I am using the following code in javascript to do this from the client:
<script type='text/javascript''> //enter refresh time in 'minutes:seconds' Minutes should range from 0 to inifinity. Seconds should range from 0 to 59 var limit='0:30' if (document.images){ var parselimit=limit.split(':') parselimit=parselimit[0]*60+parselimit[1]*1 } function beginrefresh(){ if (!document.images) return 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!' window.status=curtime setTimeout('beginrefresh()',1000) } } window.onload=beginrefresh; </script>
This works except for one thing – every time the page is refreshed, the browser (IE) gives the nag message about re-downloading content or something. This will not do. This is a page that users will have open all day and it needs to refresh without user intervention. I suppose I could do away with the message that displays the time till next refresh and just use the ASP.NET line, but if anyone has a better idea, I would really appreciate it.
Instead of using
window.location.reload(), try doing a redirect to the current URL:This way, the browser will not be doing a post back, but a new request to the server. You’ll want to test this in various browsers. I just tested FF 3 & IE8 quickly; both look good.