Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If source callback is slow, suggest may get the wrong that.val to cache #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 4 additions & 5 deletions jquery.auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@

if (!o.minChars) that.on('focus.autocomplete', function(){ that.last_val = '\n'; that.trigger('keyup.autocomplete'); });

function suggest(data){
var val = that.val();
function suggest(val, data){
that.cache[val] = data;
if (data.length && val.length >= o.minChars) {
var s = '';
Expand Down Expand Up @@ -136,14 +135,14 @@
that.last_val = val;
clearTimeout(that.timer);
if (o.cache) {
if (val in that.cache) { suggest(that.cache[val]); return; }
if (val in that.cache) { suggest(val, that.cache[val]); return; }
// no requests if previous suggestions were empty
for (var i=1; i<val.length-o.minChars; i++) {
var part = val.slice(0, val.length-i);
if (part in that.cache && !that.cache[part].length) { suggest([]); return; }
if (part in that.cache && !that.cache[part].length) { suggest(val, []); return; }
}
}
that.timer = setTimeout(function(){ o.source(val, suggest) }, o.delay);
that.timer = setTimeout(function(){ o.source(val, function (data) { suggest(val, data) }) }, o.delay);
}
} else {
that.last_val = val;
Expand Down
4 changes: 2 additions & 2 deletions jquery.auto-complete.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.