Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debouncing input (e.g. for ajax requests) #11

Closed
corford opened this issue Oct 18, 2018 · 3 comments
Closed

Debouncing input (e.g. for ajax requests) #11

corford opened this issue Oct 18, 2018 · 3 comments

Comments

@corford
Copy link

corford commented Oct 18, 2018

Looking through the source there doesn't seem to be any effort to debounce/throttle calls to the fetch() method. It would be great if an optional debounce in milliseconds could be passed as a config option. It's nice to have for ajax requests so a remote request isn't fired on every keystroke.

P.S.
Thanks for making autocomplete, it's great!

@kraaden
Copy link
Owner

kraaden commented Oct 18, 2018

Yes, the widget doesn't include throttling/debouncing, because I try to keep it as small as possible.

You can just use autocomplete with an external debounce package from NPM
https://www.npmjs.com/search?q=debounce

import { debounce } from 'some-debounce-package';

autocomplete({
    fetch: debounce(function(...) { ... }, 250),
    ...
});

@corford
Copy link
Author

corford commented Oct 18, 2018

It's true but for situations where you just want to drop in autocomplete via an external link, it's a tiny bit of extra friction for a feature that imho belongs native to the autocomplete plugin.

In terms of keeping code small as possible, I totally agree (and this is one of the main reasons I'm using your autocomplete). However, adding debounce would require about ~5 lines of code (I think?).

This:

if (val.length >= minLen) {
  settings.fetch(val, function(elements: Array<T>): void {
    if (keypressCounter === savedKeypressCounter && elements && !unloaded) {
      items = elements;
      inputValue = val;
      selected = items.length > 0 ? items[0] : undefined;
      update();
    }
  });
} else {
  clear();
}

Would become:

if (val.length >= minLen) {
  window.clearTimeout(debounceTimer);
  debounceTimer = window.setTimeout(function() {
    settings.fetch(val, function(elements: Array<T>): void {
      if (keypressCounter === savedKeypressCounter && elements && !unloaded) {
        items = elements;
        inputValue = val;
        selected = items.length > 0 ? items[0] : undefined;
        update();
      }
    });
  }, debounceWaitMs);
} else {
  clear();
}

Then add debounceTimer and debounceWaitMs vars:

const debounceWaitMs = settings.debounceWaitMs || 25;
let debounceTimer : T | undefined;

@kraaden
Copy link
Owner

kraaden commented Oct 22, 2018

added to 2.2.1

@kraaden kraaden closed this as completed Oct 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants