Can someone tell me why the javascript code below causes renewSession() to be called 7 times after a single click?
$(document).ready(function () {
$("*").mouseup(function () {
renewSession();
});
});
function renewSession() {
$.ajax({
url: "/Account/RenewSession",
type: "POST"
});
}
Probably it’s because the
mouseupevent propagates up through the DOM tree, and you’re applying the handler to every element in the document. So it’ll fire on the first element, and then the parent and so on until it gets to thehtml(or thebody, I can never quite remember without checking every time).You could use:
To prevent the multiple calls.
Edited to address comment from thiag0:
You could just target the
bodyelement; so long as events are allowed to propagate through the DOM tree (so long as you’re not callingevent.stopPropagation()on elements between the element clicked on (or ‘mouseup’-ed) then the event(s) will propagate to thebody. So I’d suggest using: