diff options
author | Vinzenz <vinzenz.rosenkranz@gmail.com> | 2016-09-10 23:10:33 +0300 |
---|---|---|
committer | Vinzenz <vinzenz.rosenkranz@gmail.com> | 2016-09-10 23:10:33 +0300 |
commit | dbfb37888e4f2b0ad8f56339b35e582ed2fcf0d5 (patch) | |
tree | 0107911d3fb4e26c0dda55eec64ff3098291b1b3 /js | |
parent | f5fadde9e27795a3d48a8101d24135bc50e445a2 (diff) |
added debounce function
Diffstat (limited to 'js')
-rw-r--r-- | js/create_edit.js | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/js/create_edit.js b/js/create_edit.js index 74cef561..853f5c52 100644 --- a/js/create_edit.js +++ b/js/create_edit.js @@ -242,13 +242,13 @@ $(document).ready(function () { } }); - $('#group-search-box').on('input', function() { - var val = $(this).val(); - if(val.length < 3) return; + $('#group-search-box').on('input', debounce(function() { var groupUl = document.getElementById('live-search-list-group-id'); while(groupUl.firstChild) { groupUl.removeChild(groupUl.firstChild); } + var val = $(this).val(); + if(val.length < 3) return; $.post(OC.generateUrl('/apps/polls/search/groups'), { searchTerm: val }, function(data) { for(var i=0; i<data.length; i++) { var gid = data[i]; @@ -259,15 +259,15 @@ $(document).ready(function () { groupUl.appendChild(li); } }); - }); + }, 250)); - $('#user-search-box').on('input', function() { - var val = $(this).val(); - if(val.length < 3) return; + $('#user-search-box').on('input', debounce(function() { var userUl = document.getElementById('live-search-list-user-id'); while(userUl.firstChild) { userUl.removeChild(userUl.firstChild); } + var val = $(this).val(); + if(val.length < 3) return; $.post(OC.generateUrl('/apps/polls/search/users'), { searchTerm: val }, function(data) { for(var i=0; i<data.length; i++) { var user = data[i]; @@ -282,7 +282,7 @@ $(document).ready(function () { userUl.appendChild(li); } }); - }); + }, 250)); $('.live-search-list-user li').each(function(){ $(this).attr('data-search-term', $(this).text().toLowerCase()); @@ -482,3 +482,19 @@ function addColToList(ts, text, dateTs) { td.id = ts; } } + +function debounce(f, wait, immediate) { + var timeout; + return function() { + var context = this; + var args = arguments; + var later = function() { + timeout = null; + if(!immediate) f.apply(context, args); + }; + var callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if(callNow) f.apply(context, args); + } +} |