The issue I saw is that a new call to the database or cache was being carried out with every keypress. This was a problem. So I found some code on http://jsbin.com/ebela4/8/edit#javascript,html and adapted it slightly. Here it is:
var delay = 600;
var isLoading = false;
var isDirty = false;
function AutoCompleteChange() {
if (!isLoading) {
// This only performs a search if four or more characters have been
// entered. Change the number to change this restriction.
if ($("#txtSearch").val().length >= 3) {
isLoading = true;
setTimeout(function () {
isLoading = false;
if (isDirty) {
isDirty = false;
// Perform actual search
// INSERT YOUR CODE HERE
}
}, delay);
};
};
};
var isLoading = false;
var isDirty = false;
function AutoCompleteChange() {
if (!isLoading) {
// This only performs a search if four or more characters have been
// entered. Change the number to change this restriction.
if ($("#txtSearch").val().length >= 3) {
isLoading = true;
setTimeout(function () {
isLoading = false;
if (isDirty) {
isDirty = false;
// Perform actual search
// INSERT YOUR CODE HERE
}
}, delay);
};
};
};
No comments:
Post a Comment