Bineet Kumar Nayak
Bineet Kumar Nayak

Reputation: 63

Jquery autocomplete:Load the source just once

I am trying a jquery autocomplete but I want the source to be loaded just once. I do not want to call ajax action class every time I type a letter. I also do not want to use cache as it is browser level cache. So Basically what I need it want to load my list just once and use autocomplete on it. Is this possible? Any ideas would be really helpful..thanks in advance

Upvotes: 1

Views: 160

Answers (2)

Lucas B
Lucas B

Reputation: 2528

Make one ajax call and build autocomplete on success :

$.getJSON({
    url: 'remote_url/',
    type: 'GET',
    success: function (data) {
      $('#autocomplete').autocomplete({
        lookup: data,
        onSelect: function (suggestion) {
          console.log(suggestion)
        }
      });
    },
    error: function (res) {
      console.log(res)
    }
  });

Upvotes: 0

João Pedro
João Pedro

Reputation: 546

You can delay the call. See: Javascript onkeyup delayed function call

var _timer = 0;
function DelayedCallMe(num) {
    if (_timer)
        window.clearTimeout(_timer);
    _timer = window.setTimeout(function() {
        callMe(num);
    }, 500);
}

Then call the function in onkeyup :

<input type="text" onkeyup="DelayedCallMe(200);"/>

Upvotes: 1

Related Questions