Solved

Javascript problem

Posted on 2014-02-24
9
264 Views
Last Modified: 2014-03-16
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

0
Comment
Question by:lburnsinmagnolia
  • 4
  • 2
9 Comments
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39883666
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

0
 

Author Comment

by:lburnsinmagnolia
ID: 39884355
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39884387
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
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 

Author Comment

by:lburnsinmagnolia
ID: 39884443
I will try make sure I don't post in an irrelevant area next time.  Thanks for the heads up.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39885131
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.
0
 

Accepted Solution

by:
lburnsinmagnolia earned 0 total points
ID: 39921524
Worked out the solution a different way.
0
 

Author Closing Comment

by:lburnsinmagnolia
ID: 39932345
no viable solutions were offered.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

809 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