diff --git a/auto-complete.js b/auto-complete.js index 3cf6dfa..ab25d7a 100644 --- a/auto-complete.js +++ b/auto-complete.js @@ -5,6 +5,9 @@ License: http://www.opensource.org/licenses/mit-license.php */ + +var requestId = 0; + var autoComplete = (function(){ // "use strict"; function autoComplete(options){ @@ -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'; @@ -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'); @@ -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; @@ -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; } }; diff --git a/package.json b/package.json new file mode 100644 index 0000000..30c1f52 --- /dev/null +++ b/package.json @@ -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" +}