Solved

Javascript problem

Posted on 2014-02-24
9
270 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
[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
  • 4
  • 2
9 Comments
 
LVL 57

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

737 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