Skip to content

Commit 4400632

Browse files
committed
Autocomplete: Improved cache demo.
1 parent 16e93d5 commit 4400632

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

demos/autocomplete/remote-with-cache.html

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,20 @@
1515
</style>
1616
<script type="text/javascript">
1717
$(function() {
18-
var cache = {};
18+
var cache = {},
19+
lastXhr;
1920
$( "#birds" ).autocomplete({
2021
minLength: 2,
2122
source: function(request, response) {
22-
if ( request.term in cache ) {
23-
response( cache[ request.term ] );
23+
var term = request.term;
24+
if ( term in cache ) {
25+
response( cache[ term ] );
2426
return;
2527
}
26-
27-
$.ajax({
28-
url: "search.php",
29-
dataType: "json",
30-
data: request,
31-
success: function( data ) {
32-
cache[ request.term ] = data;
28+
29+
lastXhr = $.getJSON( "search.php", request, function( data, status, xhr ) {
30+
cache[ term ] = data;
31+
if ( xhr === lastXhr ) {
3332
response( data );
3433
}
3534
});

0 commit comments

Comments
 (0)