Reputation: 266
I have a problem with the logic in my code and I think I've been looking at it too long to find it. When this code executes after it's 30 sec timeout, according to the debugger it seems to recursively call checkActivityStop, and makes numerous get requests to reset.aspx.
Larger picture is that I have javascript try to match the timing of the asp.net session variable. When the session variable is soon to expire, I set up a click and key press handler for the body tag that causes a get request out to a dummy page which "should" cause the session variable to be refreshed.
var sessionTime = 0;
var session;
var sessionTimer;
function resetSession() {
$.get("reset.aspx", function (data) {
$("#dbCheck").html(data);
if ($("#resetSession").length) {
clearSessionTimeout();
setSessionTimeout();
//alert("session reset");
}
});
}
function checkActivityStart() {
$("body").bind("click", resetSession());
$("body").bind("keypress", resetSession());
console.trace("checking activity");
}
function checkActivityStop() {
$("body").unbind("click", resetSession());
$("body").unbind("keypress", resetSession());
console.trace("stopping activity");
}
function setSessionTimeout() {
sessionTimer = setTimeout(function () { checkActivityStart(); }, 30000);
}
function clearSessionTimeout() {
clearTimeout(sessionTimer);
checkActivityStop();
sessionTime = 0;
}
$(document).ready(function () {
setSessionTimeout();});
Can somebody give me a second pair of eyes to help me find what is causing this recursive behavior? Hopefully, this will be enough code for someone else to see the problem but I will provide more upon request.
Upvotes: 0
Views: 165
Reputation: 167212
All these .bind()
functions are function references. But you have added them as function return values. Kindly change it to the below by removing ()
:
function checkActivityStart() {
$("body").bind("click", resetSession);
$("body").bind("keypress", resetSession);
console.trace("checking activity");
}
function checkActivityStop() {
$("body").unbind("click", resetSession);
$("body").unbind("keypress", resetSession);
console.trace("stopping activity");
}
Upvotes: 1
Reputation: 133433
You need to pass the function reference of resetSession
instead of invoking the function resetSession()
and using its return value i.e. undefined
as click handler.
$("body").bind("click", resetSession);
instead of
$("body").bind("click", resetSession());
Update your following functions
function checkActivityStart() {
$("body").bind("click", resetSession);
$("body").bind("keypress", resetSession);
console.trace("checking activity");
}
function checkActivityStop() {
$("body").unbind("click", resetSession);
$("body").unbind("keypress", resetSession);
console.trace("stopping activity");
}
Upvotes: 2