1

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

2 Answers2

1

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);"/>
Community
  • 1
  • 1
João Pedro
  • 546
  • 1
  • 5
  • 19
0

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)
    }
  });
Lucas B
  • 2,183
  • 1
  • 22
  • 22