Skip to content

Instantly share code, notes, and snippets.

@matthuhiggins
Created March 8, 2012 02:38
Show Gist options
  • Select an option

  • Save matthuhiggins/1998191 to your computer and use it in GitHub Desktop.

Select an option

Save matthuhiggins/1998191 to your computer and use it in GitHub Desktop.
delayed query
function SearchPlaces(inputSelector) {
var input = $(inputSelector).hintInput(),
baseUrl = input.attr('data-source');
function currentQuery() {
if (input.hasClass('empty')) {
return '';
} else {
return input.val();
}
}
function runQuery(url) {
$('#vote-places-wrap').addClass('loading');
$.getScript(url);
}
function delayAndQuery(query) {
var queryIfUnchanged = function() {
if (query == currentQuery()) {
runQuery(baseUrl + '?q=' + currentQuery())
}
};
setTimeout(queryIfUnchanged, 300);
}
var previousQuery = currentQuery();
setInterval(function() {
var newQuery = currentQuery();
if (previousQuery != newQuery) {
delayAndQuery(newQuery);
previousQuery = newQuery;
}
}, 150);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment