Solved

Auto refresh in ASP.NET only in client inactivity

Posted on 2010-09-05
3
1,109 Views
Last Modified: 2012-05-10
Hi,

I know that to implement auto refresh in ASP.NET just put the tag <meta http-equiv="refresh" content="ss" /> (ss is the interval in seconds to refresh). But I need that the auto refresh was executed only if there is no activity on the client during the interval of "ss" seconds.

This is possible? How do I implement it?

Regards, Marcelo Camarate
0
Comment
Question by:Marcelo Camarate
3 Comments
 
LVL 4

Expert Comment

by:senthilkumarsb
ID: 33608995
The below steps will help you very much. this code is satisfying your requirement. But this was written to close the inactive window/browser, you change the code for refresh.


Step 1. Enter script below on Between Head Tags tab in Layout HTML.
 

      <SCRIPT LANGUAGE="JavaScript">
      <!--
      var autoCloseTimer;
      var timeoutObject;
      var timePeriod = 5000; // 5 seconds
      var warnPeriod = 15000;   // 15 seconds

      function promptForClose()
      {
      autoCloseDiv.style.display = 'block';
       autoCloseTimer = setTimeout("definitelyClose()",warnPeriod);
      }


      function autoClose()
      {
       autoCloseDiv.style.display = 'block'; //shows message on page
       autoCloseTimer = setTimeout("definitelyClose()",timePeriod); //starts countdown to closure
      }

      function cancelClose()
      {
       clearTimeout(autoCloseTimer); //stops auto-close timer
       autoCloseDiv.style.display = 'none'; //hides message
      }

      function resetTimeout()
      {
        clearTimeout(timeoutObject); //stops timer
        timeoutObject = setTimeout("promptForClose()",timePeriod); //restarts timer from 0
      }


      function definitelyClose() {
          top.opener = self;
          top.window.close();
      }
      -->
      </SCRIPT>
       

Step 2. Enter following code on Inside Body Tag in Layout HTML (all on one line)

      onkeydown="resetTimeout();" onmousedown='resetTimeout();' onload="timeoutObject=setTimeout('promptForClose()',timePeriod);"

Step 3.
     a) Place a text box on your page where you want warning to appear.

     b) Press Ctrl+T and paste in the DIV code below. Feel free to modify the appearance.

     Change text to match actual time period set in script.

     The contents of the div only appear when the timer runs down.
     Clicking on cancel closes the div and resets the timer.

     <div id='autoCloseDiv' style="display:none">
       <center><p>Inactivity warning!<br>This window will autoclose in 15 seconds unless you hit 'Cancel.'</p>
       <input type='button' value='Close' onclick='definitelyClose();' />
       <input type='button' value='Cancel' onclick='cancelClose();' />
       </center>
     </div>
0
 
LVL 15

Accepted Solution

by:
StealthyDev earned 250 total points
ID: 33609396
Hi, try the below code:
<body>
	Below content till &lt;/body&gt; can be anything...
	<div style="width:300px; height:200px;border:1px solid black;">
	Testing
	</div>
	
	<input id="testInput" />

	<input />
</body>

<script>

	var __TIME_TO_AUTO_REFRESH = 5000; // update only this... this is in milli-sec. "5000=5 sec"
	var __TIME_TO_UPDATE = 50;
	var __REFRESH_AT_COUNT = __TIME_TO_AUTO_REFRESH / __TIME_TO_UPDATE;
	var __CURRENT_REFRESH_COUNT = 0;

	function updateFlag(){
		if(__CURRENT_REFRESH_COUNT > __REFRESH_AT_COUNT){
			window.location.reload();
		}
		else{
			__CURRENT_REFRESH_COUNT++;
		}
	}

	function resetAutoRefresh(){
		__CURRENT_REFRESH_COUNT = 0;
	}
	
	window.onload = function(){
		setInterval("updateFlag()", __TIME_TO_UPDATE)
	}
	
	document.onmousedown = document.onkeydown = function(){
		resetAutoRefresh();
	}

</script>

Open in new window

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33611257
What kind of activity are you thinking of? The meta-refresh timeout period will reset each time the user makes a request to the server. Or are you including AJAX calls in your inactivity?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now