Solved

Auto refresh in ASP.NET only in client inactivity

Posted on 2010-09-05
3
1,127 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
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…
Suggested Courses

617 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