Reputation: 259
I really need to understand how block the requests ajax while is pending. Let me explain an example:
I have an input type called "share" and when I write something and press enter ajax take the result and it insert inside mysql. If I do this pressing a lot of time the enter button it take more results like how many times I pressed enter even if there is a control in php. I think is a problem of the request ajax because the control php I'm sure is right. Thank you so much.
this is the code of example:
form
<input type="text" class="share">
Js
$.fn.enterKey = function (fnc) { // function for check when i press enter
return this.each(function () {
$(this).keypress(function (ev) {
var keycode = (ev.keyCode ? ev.keyCode : ev.which);
if (keycode == '13') {
fnc.call(this, ev);
}
})
})
}
$('.share').enterKey(function() { // ajax request
var comment = $(this);
if (comment.val() != "") {
$.ajax({
type: "POST",
url: add_comment.urlRemove,
data: "text=" + comment.val(),
success: function(html) {
comment.val('');
},
error: function(){
alert('Error on ajax call');
}
});
}
else {
return false;
}
});
Upvotes: 1
Views: 893
Reputation: 34846
You can set a variable to determine whether or not the request has already been sent, like this:
var alreadySent = false;
$('.share').enterKey(function() {
var comment = $(this);
if (comment.val() != "") {
if(!requestSent) {
// Set variable to true
alreadySent = true;
$.ajax({
type: "POST",
url: add_comment.urlRemove,
data: "text=" + comment.val(),
success: function(html) {
// Set variable to false
alreadySent = false;
comment.val('');
},
error: function(){
// Set variable to false
alreadySent = false;
alert('Error on ajax call');
}
});
}
}
});
Upvotes: 1