Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions ajax-type-ahead/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ fetch(endpoint)

function matchWord(wordToMatch, cities) {
return cities.filter(place => {
const regex = new RegExp(wordToMatch, 'gi');
return place.city.match(regex) || place.state.match(regex);
const regex = new RegExp(wordToMatch, 'gi');
return place.city.match(regex) || place.state.match(regex);
});
}

Expand All @@ -38,6 +38,21 @@ function displayMatch() {
searchSuggestions = searchForm.querySelector('.suggestions');
}

// debounce from https://davidwalsh.name/javascript-debounce-function
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};

searchInput.addEventListener('change', displayMatch);
searchInput.addEventListener('keyup', displayMatch);
searchInput.addEventListener('keyup', debounce(displayMatch, 200));