Solved

Javascript problem

Posted on 2014-02-24
9
267 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 56

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
Technology Partners: 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!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 …
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

735 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