Skip to content

Commit

Permalink
Added debounce for typeahead search optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
aashna27 committed Mar 3, 2019
1 parent 86d2446 commit af160a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ env:
jquery: true
node: true

parseOptions:
sourceType: "module"

# http://eslint.org/docs/rules/
rules:
# Possible Errors
Expand Down
18 changes: 18 additions & 0 deletions app/assets/javascripts/debounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
export 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);
};
};
6 changes: 3 additions & 3 deletions app/assets/javascripts/restful_typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
search API and the UI components.
Documentation here: https://github.com/bassjobsen/Bootstrap-3-Typeahead
**/
import { debounce } from './debounce.js';

$(function() {
$('input.search-query.typeahead').each(function(i, el){
$('input.search-query.typeahead').each(debounce(function(i, el){
var typeahead = $(el).typeahead({
items: 10,
minLength: 3,
delay: 350,
showCategoryHeader: true,
autoSelect: false,
source: function (query, process) {
Expand Down Expand Up @@ -46,5 +46,5 @@ $(function() {
showAll: true
}
});
});
}, 350));
});

0 comments on commit af160a5

Please sign in to comment.