Skip to content

Instantly share code, notes, and snippets.

@skoon
Forked from howarddierking/gist:5561466
Created May 11, 2013 22:11
Show Gist options
  • Save skoon/5561609 to your computer and use it in GitHub Desktop.
Save skoon/5561609 to your computer and use it in GitHub Desktop.
var searchForSubject = function(model, event){
_subjectSearchText = event.srcElement.value;
console.log(_subjectSearchText);
// simple first algorithm -
// wait for 1 second
// search if
// the search string is non-falsey
// the string at the start of the timeout is the same as the current
// the search string is at least 3 characters
// else
// wait for 1 more second and repeat
var id = setTimeout(function(originalSearchText){
if(_subjectSearchText
&& _subjectSearchText==originalSearchText
&& _subjectSearchText.length >= 3){
if(_searching)
console.log('already searching');
else {
_searching = true;
_searchForSubject(_subjectSearchText);
// reset the searching variable in 1 second to give straggler timeouts time to complete
setTimeout(function(){ _searching = false; }, 1000);
}
} else {
if(_searching)
console.log('already searching');
else
console.log('nope, too much has changed between ' + originalSearchText + ' and ' + _subjectSearchText);
}
}, 1000, _subjectSearchText);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment