Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Drop response of old requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Osvaldo Matos-Junior authored and rafaelverger committed Feb 15, 2017
1 parent da3885c commit 619a150
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 16 additions & 1 deletion 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";

Expand Down Expand Up @@ -75,6 +78,7 @@ var autoComplete = (function(){
that.setAttribute('autocomplete', 'off');
that.cache = {};
that.last_val = '';
that._currentRequestId = 0;

that.updateSC = function(resize, next){
var rect = that.getBoundingClientRect();
Expand Down Expand Up @@ -203,7 +207,18 @@ 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 = requestId++;
var suggestWrap = function(data) {
// drop response of old requests
if (thisRequestId < that._currentRequestId) {
return;
}
that._currentRequestId = thisRequestId;
return suggest(data);
}
o.source(val, suggestWrap);
}, o.delay);
}
} else {
that.last_val = val;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-autocomplete",
"version": "1.0.1",
"version": "1.0.2",
"description": "An extremely lightweight and powerful vanilla JavaScript completion suggester.",
"main": "auto-complete.js",
"scripts": {
Expand Down

0 comments on commit 619a150

Please sign in to comment.