Solved

Auto refresh in ASP.NET only in client inactivity

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp web application 3 43
Accordion won't close correctly 5 14
Set the FileVersion of a website dll 1 28
How do i use the await event in php echo 5 19
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

733 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