Giter Club home page Giter Club logo

throttleit's Introduction

throttleit

Throttle a function to limit its execution rate

Install

npm install throttleit

Usage

import throttle from 'throttleit';

// Throttling a function that processes data.
function processData(data) {
	console.log('Processing:', data);

	// Add data processing logic here.
}

// Throttle the `processData` function to be called at most once every 3 seconds.
const throttledProcessData = throttle(processData, 3000);

// Simulate calling the function multiple times with different data.
throttledProcessData('Data 1');
throttledProcessData('Data 2');
throttledProcessData('Data 3');

API

throttle(function, wait)

Creates a throttled function that limits calls to the original function to at most once every wait milliseconds. It guarantees execution after the final invocation and maintains the last context (this) and arguments.

function

Type: function

The function to be throttled.

wait

Type: number

The number of milliseconds to throttle invocations to.

Related

  • p-throttle - Throttle async functions
  • debounce - Delay function calls until a set time elapses after the last invocation

throttleit's People

Contributors

calvinfo avatar darsain avatar juliangruber avatar mrmartineau avatar sindresorhus avatar tj avatar tootallnate avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

throttleit's Issues

Last call to throttled function may be dropped

There are cases where the last call to the throttled function are critical to the behavior of an application.

For example when used to throttle scroll events.

Say we want to throttle scroll events to one per 100ms.

  • 0ms - first scroll event occurs and calls throttled function, triggers UI update
  • 1ms - 99ms scroll events occur - throttle drops them as time hasn't expired
  • 100ms - next scroll event makes it through throttle, resetting time and triggers UI update
  • 120ms - scrolling stops

In this scenario, the last scroll event never makes it through leaving the UI out of sync with the actual scroll position.

Perhaps a better model for this would be underscores' implementation that will call the trailing edge unless requested not to.

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.