Skip to content

Tupy/drop old responses #5

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

Closed
wants to merge 3 commits into from
Closed
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
20 changes: 16 additions & 4 deletions auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
License: http://www.opensource.org/licenses/mit-license.php
*/


var requestId = 0;

var autoComplete = (function(){
// "use strict";
function autoComplete(options){
Expand Down Expand Up @@ -63,7 +66,6 @@ var autoComplete = (function(){
that.updateSC = function(resize, next){
var rect = that.getBoundingClientRect();
that.sc.style.left = rect.left + (window.pageXOffset || document.documentElement.scrollLeft) + 'px';
that.sc.style.top = rect.bottom + (window.pageYOffset || document.documentElement.scrollTop) + 1 + 'px';
that.sc.style.width = rect.right - rect.left + 'px'; // outerWidth
if (!resize) {
that.sc.style.display = 'block';
Expand All @@ -81,7 +83,7 @@ var autoComplete = (function(){
}
}
addEvent(window, 'resize', that.updateSC);
document.body.appendChild(that.sc);
that.parentElement.appendChild(that.sc);

live('autocomplete-suggestion', 'mouseleave', function(e){
var sel = that.sc.querySelector('.autocomplete-suggestion.selected');
Expand Down Expand Up @@ -173,7 +175,17 @@ var autoComplete = (function(){
if (part in that.cache && !that.cache[part].length) { suggest([]); return; }
}
}
that.timer = setTimeout(function(){ o.source(val, suggest) }, o.delay);
that.timer = setTimeout(function(){
var thisRequestId = that._currentRequestId = requestId++;
var suggestWrap = function(data) {
// drop response of old requests
if (thisRequestId !== that._currentRequestId) {
return;
}
return suggest(data);
}
o.source(val, suggestWrap);
}, o.delay);
}
} else {
that.last_val = val;
Expand Down Expand Up @@ -203,7 +215,7 @@ var autoComplete = (function(){
that.setAttribute('autocomplete', that.autocompleteAttr);
else
that.removeAttribute('autocomplete');
document.body.removeChild(that.sc);
that.parentElement.removeChild(that.sc);
that = null;
}
};
Expand Down
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "javascript-autocomplete",
"version": "1.0.0",
"description": "An extremely lightweight and powerful vanilla JavaScript completion suggester.",
"main": "auto-complete.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jusbrasil/JavaScript-autoComplete.git"
},
"keywords": [
"autocomplete",
"vanilla",
"javascript"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/jusbrasil/JavaScript-autoComplete/issues"
},
"homepage": "https://github.com/jusbrasil/JavaScript-autoComplete#readme"
}