npm.io
1.0.2 • Published 3 years ago

@simulacron/tools

Licence
BSD-3-Clause
Version
1.0.2
Deps
0
Size
6 kB
Vulns
0
Weekly
0

A set of helper functions.

debounce & throttle

debounce

Debounce the execution of the function.

You can specify the value in milliseconds as a number or in string format with the suffix '<delay>ms', supported suffixes: ms - milliseconds, s - seconds, m - minutes.

This can be handy for events such as key presses or "input" in input fields.

debounce(
  inputChanged(event) {
  // ...
  },
  '300ms'
);
throttle

Throttle the execution of the function.

You can specify the value in milliseconds as a number or in string format with the suffix '<delay>ms', supported suffixes: ms - milliseconds, s - seconds, m - minutes.

This can be handy for "resize" or "scroll" events.

throttle(
  resizeChanged(event) {
    // ...
  },
  '500ms'
);
duration

Duration converter from human-readable form to milliseconds.

Converts a string like '<delay>ms' to milliseconds. Supported suffixes: ms - milliseconds, s - seconds, m - minutes.

If a numeric value is passed, it is returned unchanged.

const delayInMilliseconds = duration('500ms');

Keywords