Solved

Auto refresh in ASP.NET only in client inactivity

Posted on 2010-09-05
3
1,105 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

758 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

22 Experts available now in Live!

Get 1:1 Help Now