Coder Social home page Coder Social logo

throttle-debounce's Introduction

throttle-debounce

Build Status Mentioned in Awesome Micro npm Packages

Throttle and debounce functions.

This module is the same as jquery-throttle-debounce (with some differences), but it’s transferred to ES Modules and CommonJS format.

Install

npm install throttle-debounce --save

Usage

import { throttle, debounce } from 'throttle-debounce';

throttle(300, () => {
	// Throttled function
});

debounce(300, () => {
	// Debounced function
});

cancelling

Debounce and throttle can both be cancelled by calling the cancel function.

 const throttled = throttle(300, () => {
	// Throttled function
});
throttled.cancel();

The logic that is being throttled or debounced will no longer be called.

API

throttle(delay, noTrailing, callback, debounceMode)

Returns: Function

Throttle execution of a function. Especially useful for rate limiting execution of handlers on events like resize and scroll.

delay

Type: Number

A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.

noTrailing

Type: Boolean

Optional, defaults to false. If noTrailing is true, callback will only execute every delay milliseconds while the throttled-function is being called. If noTrailing is false or unspecified, callback will be executed one final time after the last throttled-function call. (After the throttled-function has not been called for delay milliseconds, the internal counter is reset)

callback

Type: Function

A function to be executed after delay milliseconds. The this context and all arguments are passed through, as-is, to callback when the throttled-function is executed.

debounceMode

Type: Boolean

If debounceMode is true (at begin), schedule clear to execute after delay ms. If debounceMode is false (at end), schedule callback to execute after delay ms.

debounce(delay, atBegin, callback)

Returns: Function

Debounce execution of a function. Debouncing, unlike throttling, guarantees that a function is only executed a single time, either at the very beginning of a series of calls, or at the very end.

delay

Type: Number

A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.

atBegin

Type: Boolean

Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed delay milliseconds after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call. (After the throttled-function has not been called for delay milliseconds, the internal counter is reset).

callback

Type: Function

A function to be executed after delay milliseconds. The this context and all arguments are passed through, as-is, to callback when the debounced-function is executed.

Differences with original module

  • Dependancy on jQuery is removed, so if you rely on GUIDs set by jQuery, plan accordingly
  • There is no standalone version available, so don’t rely on $.throttle and $.debounce to be available

Browser support

Tested in IE9+ and all modern browsers.

Test

For local automated tests, run npm run test:automated:local (append :watch for watcher support).

License

Original module license: Copyright (c) 2010 "Cowboy" Ben Alman (Dual licensed under the MIT and GPL licenses. http://benalman.com/about/license/)
This module license: MIT © Ivan Nikolić

throttle-debounce's People

Contributors

a-korzun avatar cgnonofr avatar eightypop avatar nikitaeverywhere avatar niksy avatar ossdev07 avatar plee-nm avatar tedwu1027 avatar yuu2lee4 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.