Skip to content

Commit 198a983

Browse files
Add debounce to search
This makes the UI feel far less laggy while typing in a search query. It avoids applying redundant searches, which can easily add up to ~150ms per character on an older device or mobile phone.
1 parent b71c13c commit 198a983

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

js/main.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,23 @@ $('#project-list').
33
css({visibility: 'visible'});
44
$('footer').css({visibility: 'visible'});
55

6-
$('#search-query').on('change keyup paste input', function(event) {
7-
var query = $.trim(event.target.value.toLowerCase());
6+
function debounce(f, wait){
7+
var timeout;
8+
9+
return function executedFunction() {
10+
var args = arguments;
11+
var that = this;
12+
var later = function(){
13+
clearTimeout(timeout);
14+
f.apply(that, args);
15+
};
16+
17+
clearTimeout(timeout);
18+
timeout = setTimeout(later, wait);
19+
};
20+
};
21+
22+
function applySearch(query){
823
var projects = $('#project-list li');
924
if (query == '') {
1025
projects.show();
@@ -13,6 +28,13 @@ $('#search-query').on('change keyup paste input', function(event) {
1328
}
1429
$('#project-list .category').show().not(':has(li:visible)').hide();
1530
$('#project-list').masonry('layout');
31+
}
32+
33+
var debouncedSearch = debounce(applySearch, 100);
34+
35+
$('#search-query').on('change keyup paste input', function(event) {
36+
var query = $.trim(event.target.value.toLowerCase());
37+
debouncedSearch(query)
1638
});
1739

1840
$('#search-query').focus().change();

0 commit comments

Comments
 (0)