I have an input field. When there is a keyup on this field, I send a request . My problem is when another keyup event is triggered, I need to cancel all pending requests. I have seen a lot of answers, but I have not found a solution.
var data = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
wildcard: '%QUERY',
url: $('.typeahead').attr('data-autocomplete-url') + '?term=%QUERY',
rateLimitBy: 'throttle',
ajax: {
async: true,
dataType: 'json',
crossDomain: true,
}
}
});
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},{
name: 'company',
displayKey: 'id',
limit: Infinity,
source: data,
templates: {
empty: [
'<div style="margin: 4px 30px 4px 30px;" class="empty-message">',
' Aucun résultat trouvé ',
'</div>'
].join('\n'),
suggestion: function(data) {
if(data.id_db == 'more'){
return '<p style="pointer-events:none">'+ data.text +'</p>';
}else{
return '<p>'+ data.text +'</p>';
}
}
}
}).on('typeahead:select', function(ev, data) {
$('#id_db').val(data.id_db);
changeCompany($(this));
});