Hi All,
I have an app that includes a search feature. This feature is implemented by having a user type a term in a search bar. Once the user enters 3 characters an ajax call is started. I thought that this would be enough, but it turns out that there's a bug that I think has to do with timing and the fact that several ajax calls can be done in quick succession by just typing a search term over 4 characters.
What I would like to do is this:
- User starts typing
- script checks for 3+ characters
- once 3 characters are typed, wait (using setTimeout perhaps)
- once timeout is reached, check if more characters have been entered
- loop 3 & 4 until no more characters have been entered for 1-2 seconds
- do ajax call ... rest is all set ...
my question: I know how I would do everything except check for more characters coming in. maybe something like this:
var searchTerm;
function wait(userInput){
if(userInput.length < 3){return 0;}
searchTerm = userInput;
setTimeout(function(){checkInput(userInput);}, 1000);
}
function checkInput(userInput){
if (userInput == searchTerm){
... do AJAX call ...
} else {
return 0;
}
}
That's just off the cuff and probably wouldn't run right away. Anyways, any help, tips or direction would be greatly appreciated.
Thanks, Tim