At the beginning, you should create a new global variable under paginam
, like so:
var paginam = 1,
contactRequest = false;
Then, change your listcontactosmsj()
function to the following:
function listcontactosmsj(contacto, paginam) {
if (contactRequest) {
contactRequest.abort();
}
contactRequest = $.ajax({
type: 'GET',
url: "<? echo PUBLIC_PATH . 'message/selcontacts' ?>",
data: 'contacto=' + contacto
+ '&pagina=' + paginam
}).done(function(data) {
$('div.dvusuarios').append(data);
contactRequest = false;
});
}
You're right about what's happening. When you type quickly, the keyup
event fires rapidly, and multiple AJAX requests are being sent rapidly. With this modification, the listcontactosmsj()
function will check to see if there's currently a request being made. If there is, it cancels the request, and starts a new one.