Skip to content

Instantly share code, notes, and snippets.

@howarddierking
Last active December 17, 2015 05:58
Show Gist options
  • Save howarddierking/5561466 to your computer and use it in GitHub Desktop.
Save howarddierking/5561466 to your computer and use it in GitHub Desktop.
function for triggering search as the user is typing
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