Solved

Javascript problem

Posted on 2014-02-24
9
261 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 51

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now