Reputation:
I have the following function which fetches more comments beyond the 20 that are shown by default:
$('.more_comments_link').live('click', function() {
$(".more_comments_link").text("Fetching More Comments...");
var ajaxOpts = {
type: "get",
url: "ajax_getcomments.php",
dataType: 'json',
data: "&itemid=<? echo $id; ?>&type=1&owner=<? echo $event["data"]["e_creator"]; ?>&more=1",
success: function(data) {
$('.discussion-more').after(data);
$(".discussion-more").hide();
}
};
$.ajax(ajaxOpts);
return false;
});
It works, but the only problem is, the user can click the button three times really quickly, and it will send three requests to ajax_getcomments.php, getting the same result set every time.
I tried adding
$(".more_comments_link").unbind('click');
but it doesn't do anything.
The initial result set is also fetched with jQuery, hence I’m using .live(click'
.
I am not sure if it has anything to do with why it’s not working.
Upvotes: 19
Views: 46885
Reputation: 79
For me this worked:
$('.more_comments_link').live('click', function() {
if(e.handled !== true) // This will prevent event firing more than one time
{
e.handled = true;
// Your code here...
}
});
Upvotes: -1
Reputation: 425
You can use:
$('element').off()
This will disable any event of your component that you are listening to.
Upvotes: 2
Reputation: 842
My solution, working global on all Ajax requests. If it is the active request, new with the same URL will be aborted. It is working fine.
var activeRequests = [];
$(document).ajaxComplete(function(e, jqxhr, settings) {
var i = activeRequests.indexOf(settings.url);
if (i != -1) {
setTimeout(function(){
activeRequests.splice(activeRequests.indexOf(settings.url), 1); }, 75);
}
});
$(document).ajaxSend(function(e, jqxhr, settings) {
var i = activeRequests.indexOf(settings.url)
if (i != -1) {
jqxhr.abort();
activeRequests.push(settings.url);
}
});
Upvotes: 1
Reputation: 5367
All of these solutions are far too complex for a very simple solution; use .one():
Simply change live
to one
. See below:
$('.more_comments_link').one('click', function() {
Upvotes: 16
Reputation: 2942
Try using the disabled keyword. This way you don't have to unbind the function; you just disable it from being pressed:
$('.more_comments_link').live('click', function() {
$(this).attr("disabled", true);
$(".more_comments_link").text("Fetching More Comments ...");
var ajaxOpts = {
type: "get",
url: "ajax_getcomments.php",
dataType: 'json',
data: "&itemid=<? echo $id; ?>&type=1&owner=<? echo $event["data"]["e_creator"]; ?>&more=1",
success: function(data) {
$('.discussion-more').after(data);
$(".discussion-more").hide();
}
};
$.ajax(ajaxOpts);
return false;
});
Upvotes: 1
Reputation: 57
You Can use for a single click on a button over the webpage
$('button id or classs').one('click',function(){
});
Upvotes: 0
Reputation: 4687
Depending on your use case, you might not have to use live, in which case one would do exactly what you want it to do.
Upvotes: 1
Reputation: 238115
live
doesn't work with unbind
-- you need die
instead.
However there is a better solution. Since you probably want to be able to update the content more than once, you can set a variable to see whether a request is currently running:
var requestRunning = false;
$('.more_comments_link').live('click', function () {
if (requestRunning) { // don't do anything if an AJAX request is pending
return;
}
$(".more_comments_link").text("Fetching More Comments ...");
var ajaxOpts = {
type: "get",
url: "ajax_getcomments.php",
dataType: 'json',
data: "&itemid=<? echo $id; ?>&type=1&owner=<? echo $event["
data "]["
e_creator "]; ?>&more=1",
success: function (data) {
$('.discussion-more').after(data);
$(".discussion-more").hide();
},
complete: function() {
requestRunning = false;
}
};
requestRunning = true;
$.ajax(ajaxOpts);
return false;
});
Upvotes: 34
Reputation: 15237
$('.more_comments_link').live('click', function(e) {
if($(e.target).data('oneclicked')!='yes') {
$(this).text("Fetching More Comments ...");
var ajaxOpts = {
type: "get",
url: "ajax_getcomments.php",
dataType: 'json',
data: "&itemid=<? echo $id; ?>&type=1&owner=<? echo $event["data"]["e_creator"]; ?>&more=1",
success: function(data) {
$('.discussion-more').after(data);
$(".discussion-more").hide();
}
};
$.ajax(ajaxOpts);
return false;
}
$(e.target).data('oneclicked','yes');
});
Upvotes: 0