Link to home
Start Free TrialLog in
Avatar of lburnsinmagnolia
lburnsinmagnolia

asked on

Javascript problem

I have the following javascript to "auto-logout" a user on a web site after a period on inactivity.  The problem is, that the timeout routine in code-behind fires constantly.
Can anyone tell me why the routine is being called when it should not?

If you'll note, the timeout should not execute until 3 minutes of inactivity.

<script type="text/javascript">

   // var idleTime = 0;

    $(window).load(function () {
        var authenticated = '<%= Session("IsAuthenticated")%>';
        if (authenticated == "Y") {
            idleTime = 0;           //Increment the idle time counter every minute.
            var idleInterval = setInterval(timerIncrement(idleTime), 60000); // 1 minute

            //Zero the idle timer on mouse movement.
            $(this).mousemove(function (e) {
                idleTime = 0;
            });
            $(this).keypress(function (e) {
                idleTime = 0;
            });
        }
    });

    function timerIncrement(idleTime) {

        idleTime = idleTime + 1;
        if (idleTime >= 3)
        { // 3 minutes
            idleTime = 0;
            <%timeout()%>
        }
    }
  
 
</script> 

Open in new window


' code behind

  Protected Sub timeout()
               Session("IsAuthenticated") = "N"
    End Sub

Open in new window

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

There are a number of problems here

1. Javascript cannot call code behind directly - you have to do it through something like an AJAX call or a redirect to another page.

The following code
<%timeout()%>

Open in new window

Will call timeout at the time the script is run not at the time the timeout expires. Server side scripts complete before the page loads in the browser so any code in the server side code is no longer available at the time the javascript executes.

2. setInterval is called in one of two ways

i. Using a string parameter with the function to call as the string (in this case 'timerIncrement()')
setInterval('timerIncrement()', 1000);

Open in new window

ii. A function that is executed when the timeout fires
setInterval(function() { timerIncrement();}, 1000);

Open in new window

If you use the second option you can call timerIncrement with a parameter like idleTime but it won't work because Javascript functions pass parameters by value which means that a copy of the variable is passed to the function. If the function modifies the variable it is the copy that is changed not the original.
So in this case idleTime won't be affected and will remain 0 on each call to the timerInterval.
A better approach was your original one which is to use a global variable to keep track of idleTime.

Here is a suggestion on how you should do this
<script type="text/javascript">
var idleTime = 0;
$(window).load(function () {
	var authenticated = '<%= Session("IsAuthenticated")%>';
	if (authenticated == "Y") {
		var idleInterval = setInterval('timerIncrement()', 1000); // 1 minute

		//Zero the idle timer on mouse movement.
		$(this).mousemove(function (e) {
			idleTime = 0;
		});
		$(this).keypress(function (e) {
			idleTime = 0;
		});
	}
});

function timerIncrement() 
{
	if (idleTime++ >= 3)
	{ // 3 minutes
		idleTime = 0;
		window.location = "logoff.asp"
	}
}

Open in new window

And then create the following ASP script to handle the logoff
logoff.asp
  Session("IsAuthenticated") = "N"
  'You can optionally include a redirect here back to your login page
  ' Example
  ' Response.Redirect login.asp

Open in new window

Avatar of lburnsinmagnolia
lburnsinmagnolia

ASKER

To CEHJ,

I know that javascript is not a language like Java.  Just like your link does nothing to solve my problem. I posted the question to all those categories hoping that people monitoring questions, even they only program in Java, might have scripts to help solve the problem.

JulianH,

I will try your suggestion tomorrow when I get back to the office.

Thanks
I know that javascript is not a language like Java.  
Strange then that you should post your question in an irrelevant TA, instead of, say, one of the many web programming TAs
I will try make sure I don't post in an irrelevant area next time.  Thanks for the heads up.
discourages the posting of blind links

It's not a 'blind' link - it's a link to show burnsinmagnolia how s/he might use this site more efficiently by recognising that posting JavaScript questions in the Java TA is not appropriate.
ASKER CERTIFIED SOLUTION
Avatar of lburnsinmagnolia
lburnsinmagnolia

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
no viable solutions were offered.