1

I'm looking to a jQuery/javascript method for doing theses 2 functionnalities in one unique event :

  • an abort of an XHR query when a new one is sent
  • a timeout to avoid a lot of queries at each keypress

$('#myElement').on('input propertychange', function() {
  if (window.xhr) { window.xhr.abort(); }
  if (window.timer_xhr) { window.clearTimeout(window.timer_xhr); }
  window.timer_xhr = setTimeout(function() {
    window.xhr = $.get( /* some params */ );
  }, 200);
});

This code works perfeclty, but I wrote it so many times... There has to be a method doing this somewhere... (and if you have any remarks about this code, you can give it to me)

I'm looking to a method to declare like this :

$('#myElement').onChangeWithTimeout(function() { $.get(...) },
  200 /* additional parameter */
)
pierallard
  • 3,326
  • 3
  • 21
  • 48

0 Answers0