Thursday, 8 August 2013

How to kill a JQuery function while it's running

How to kill a JQuery function while it's running

I've written a script that checks to see if the user is active on a page,
if not a pop up is displayed after 15 minutes, at which point a timer is
displayed which counts down from 60 seconds, and then redirects the user
to a page that kills the users session on the website.
However, while I can reset the timer that displays the pop up window, I
can't seem to be able to cancel the second timer that counts down from 60.
Here is my code:
<script type="text/javascript">
idleTime = 0;
idleRedirect = 0;
// Count for the timer
var wsCount = 60;
// Start Countdown
function startCountdown() {
if((wsCount - 1) >= 0){
wsCount = wsCount - 1;
// Display countdown
$("#countdown").html('<span id="timecount">' + wsCount +
'</span>.');
timing = setTimeout(startCountdown, 1000);
}
else{
// Redirect to session kill
alert('Goodbye');
}
}
// Pop up when the timer hits 15 minutes
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 1) { // 15 minutes
$('#inactiveWarning').modal('show');
startCountdown();
}
}
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval("timerIncrement()", 5000); // 1
minute
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
idleRedirect = 0;
startCountdown.die();
});
$(this).keypress(function (e) {
idleTime = 0;
idleRedirect = 0;
startCountdown.die();
});
$(this).scroll(function (e) {
idleTime = 0;
idleRedirect = 0;
startCountdown.die();
});
})
</script>

No comments:

Post a Comment