Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Auto refresh in ASP.NET only in client inactivity

Posted on 2010-09-05
3
Medium Priority
?
1,136 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…
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…

715 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