Thursday, 12 September 2013

Ajax, prevent multiple request on click

Ajax, prevent multiple request on click

I'm trying to prevent multiple requests when user click on login or
register button. This is my code, but it doesn't work. Just the first time
works fine, then return false..
$('#do-login').click(function(e) {
e.preventDefault();
if ( $(this).data('requestRunning') ) {
return;
}
$(this).data('requestRunning', true);
$.ajax({
type: "POST",
url: "/php/auth/login.php",
data: $("#login-form").serialize(),
success: function(msg) {
//stuffs
},
complete: function() {
$(this).data('requestRunning', false);
}
});
});
Any ideas? Thanks!

No comments:

Post a Comment