bitty installation snippets examples documentation q&a

snippets

debounce a signal

  • b.debounce() can be used to debounce signals.
  • It takes a key, signals to be called, and a timeout.
  • Each time b.debounce() is called with a given key the timeout is reset to specified value.
  • You can click the button in this snippet multiple times. It only increments the counter once per time the debounce is allowed to finish and resolves.

html

<button data-s="start" data-r="end">Start</button>

javascript

export const b = {};

let count = 0;

export function start(ev, __, el) {
  ev.target.innerHTML = "waiting for debounce to resolve";
  b.debounce("snippet", "end", 2000);
}

export function end(_, __, el) {
  count += 1;
  el.innerHTML = `debounce has resolved to count ${count}`;
}