dettle
A tiny fully-featured debounce and throttle implementation.
Last updated a year ago by fabiospampinato .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install dettle 
SYNC missed versions from official npm registry.

Dettle

A tiny fully-featured debounce and throttle implementation.

Install

npm install --save dettle

Usage

import {debounce, throttle} from 'dettle';

const fn = () => console.log ( 'Fired!' );

// Debouncing
// The following options are supported:
// `leading`: whether the function should be called when the timeout is created, defaults to `false`
// `trailing`: whether the function should be called when the timeout expires, defaults to `true`
// `maxWait`: the maximum amount of time that can pass before the function is called, defaults to `Infinity`

const debounced = debounce ( fn, 1000, {
  leading: false,
  trailing: true,
  maxWait: Infinity
});

debounced (); // Schedule function for execution
debounced (); // Re-schedule function for execution

debounced.flush (); // Execute the function immediately, if there's a scheduled execution
debounced.cancel (); // Cancel the scheduled execution

// Throttling
// The API for throttling is basically the same, except that:
// - `leading`: is `true` by default rather than `false`
// - `maxWait`: is set implicitly for you to be equal to the wait time

const throttled = throttle ( fn, 1000, {
  leading: true,
  trailing: true
});

throttled (); // Call the function immediately
throttled (); // Schedule function for execution

throttled.flush (); // Execute the function immediately, if there's a scheduled execution
throttled.cancel (); // Cancel the scheduled execution

License

MIT © Fabio Spampinato

Current Tags

  • 1.0.5                                ...           latest (a year ago)

6 Versions

  • 1.0.5                                ...           a year ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 2
This Month 3
Last Day 2
Last Week 1
Last Month 0
Dependencies (0)
None
Dev Dependencies (3)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |