diff options
| author | luccioman <luccioman@users.noreply.github.com> | 2018-05-22 07:55:09 +0200 |
|---|---|---|
| committer | luccioman <luccioman@users.noreply.github.com> | 2018-05-22 07:55:09 +0200 |
| commit | 1dfd3e9dde75003fa62fd853b6a1ee96f2e669bc (patch) | |
| tree | 0af00400da281ab149b68f12e7e7c50c7a3d6344 /htroot/yacysearch.html | |
| parent | 84d82bfdd7aff2a086e6f1fcd7f75ebff43b4741 (diff) | |
Limit the rate of calls to the suggest API when typing in search field
Diffstat (limited to 'htroot/yacysearch.html')
| -rw-r--r-- | htroot/yacysearch.html | 54 |
1 files changed, 30 insertions, 24 deletions
diff --git a/htroot/yacysearch.html b/htroot/yacysearch.html index 687439f25..deb616439 100644 --- a/htroot/yacysearch.html +++ b/htroot/yacysearch.html @@ -22,25 +22,6 @@ <link rel="stylesheet" type="text/css" media="screen" href="env/yacysort.css" /> #(/jsResort)# <script type="text/javascript"> - var suggestMatcher = function() { - return function opensearch(q, cb) { - $.getJSON("suggest.json?q=" + q, function(data) { - var parsed = []; - for (var i = 0; i < data[1].length; i++) { - var row = data[1][i]; - if (row) { - parsed[parsed.length] = { - data: [row], - value: row, - result: row - }; - }; - }; - cb(parsed); - }); - }; - }; - $(document).ready(function() { #(jsResort)#:: $.get( @@ -53,11 +34,36 @@ ); #(/jsResort)# - $('#search').typeahead({hint:false,highlight:true,minLength:1}, { - name: 'states', - displayKey: 'value', - source: suggestMatcher() - }); + var suggestTimeoutId = null; + + /* Configure the search input field to get suggestions on key strokes */ + $('#search').typeahead({hint:false,highlight:true,minLength:1}, { + name: 'states', + displayKey: 'value', + source: function(query, render) { + if(suggestTimeoutId != null) { + /* Remove delayed call not yet done */ + clearTimeout(suggestTimeoutId); + } + /* Limit the rate of calls to the suggest API by adding a delay before effective call */ + suggestTimeoutId = setTimeout(function() { + $.getJSON("suggest.json?q=" + query, function(data) { + var parsed = []; + for (var i = 0; i < data[1].length; i++) { + var row = data[1][i]; + if (row) { + parsed[parsed.length] = { + data: [row], + value: row, + result: row + }; + }; + }; + render(parsed); + }); + }, 300); + } + }); }); </script> <style type="text/css">.twitter-typeahead {margin: 0px;padding: 0px;top:2px;}</style> <!-- fix for input window --> |
