Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi experts,

please can you explain me this javascript code.

This is code is used for redirecting to login page after 15 minutes, if the user is idle in asp.net project

XML
<script type="text/javascript">
function InitSessionTimer() {
    /* mReschke 2010-09-29 */
    warn_sec = 59 * 60 * 1000;             //Warning time in milliseconds
    timeout_sec = 60 * 60 * 1000;          //Actual timeout in milliseconds
    show_warning = true;
    epoch = new Date().getTime();
    CheckSessionStatus();
}
InitSessionTimer();
function CheckSessionStatus() {
    /* mReschke 2010-09-29 */

    //Check for session warning
    epoch2 = new Date().getTime();
    if (epoch2 > epoch + warn_sec && epoch2 < epoch + timeout_sec && show_warning) {
        show_warning = false; //Don't show again
        alert_shown = true;
        alert("Your session will timeout in " + Math.round((timeout_sec - warn_sec) / 1000) + " minute, please click a button or navigate to another page to refresh your session before it expires.");
        down = setTimeout("CheckSessionStatus();", 1000);
    } else if (epoch2 > epoch + timeout_sec) {
        alert("Sorry, Your session has timed out.");
        window.location.href = 'Login.aspx';
    } else {
        down = setTimeout("CheckSessionStatus();", 1000);
    }
}
</script>


---------------------------------------

please explain me this line of code.

warn_sec = 59 * 60 * 1000;

what is 59, what is 60 and what is 1000 here.

Please explain thanks.

I want to redirect to Login.aspx page if user is idle after 15 minutes.
Posted
Comments
Sergey Alexandrovich Kryukov 21-Aug-13 1:16am    
Wrong approach in principle. Who knows what kind of trash can you find in the Web? (This 59 vs. 60 looks like yet another stupidity to me, by the way...)

Ask the person who wrote it, or, much better, write your own code.
—SA

it will warn upto 59 minutes to click on button and just after 59 minutes it will redirect to login page.


1000 means 1 second
1000 * 60 means 1 minute
1000 * 60 * 59 means 59 minutes




You want code for 15 minutes. okyeee, let me explain

1000 = 1 second (1000 is miliseconds)
1000 * 60 = 60000 miliseconds (60 seconds)
1000 * 60 * 15 = 900000 (15 minutes)
 
Share this answer
 
v2
Comments
HYD Webdeveloper 21-Aug-13 1:40am    
how to set 15 minutes....please help
Dineshshp 21-Aug-13 1:44am    
You want code for 15 minutes, okyeee, let me explain
1000 means 1 second
1000 * 60 means 1 minute
1000 * 60 * 15 means 15 minutes

1000*60*15 = 900000 (900000 is miliseconds)
900000 / 1000 = 900 seconds
900 / 60 = 15 minutes
Vikas Desai 21-Aug-13 2:02am    
15 min = 15 * 60 seconds = 15 * 60 *1000 milliseconds i.e. (900000 milliseconds)
Maciej Los 21-Aug-13 2:06am    
+5
59 * 60 * 1000
59 is in minutes
60 to covert minutes in seconds
and 1000 to convert the seconds in milliseconds.


To answer why there is 59...

59 is the last number post which it gets converted to Hour (if it would be 60 then it would be counted as hour).
So its 59 there.

warn_sec = 59 * 60 * 1000 - this is to warn on 59th min
timeout_sec = 60 * 60 * 1000 - will time out when it reaches 60th min i.e 1 hour

usually this kind of script is used in cyber cafe now a days to warn the user when it is about to reach an hour.
 
Share this answer
 
v3
The below code can be used to redirect user after predefined time interval.
Code:
XML
string script = "<SCRIPT Language='JavaScript'> ";
script += "redirection()";
script += "</SCRIPT>";
Page.RegisterClientScriptBlock("ClientScript", script);

Javascript (Add it to the head of the page):
XML
<script type="text/javascript">
     function redirection()
     {
        alert('You wil be redirected to Google.com after 10 seconds.');
        setTimeout("top.location.href = 'http://www.google.com'", 10000);
     }
</script>

Regards..:)
 
Share this answer
 
Comments
HYD Webdeveloper 21-Aug-13 4:21am    
explain me this ?

timeout_sec = 60 * 60 * 1000;

What is 60, what is 60, and what is 1000 in these timeout_sec
Thanks7872 21-Aug-13 5:14am    
Why don't you have a look at above answer and even a comment?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900