Socket
Socket
Sign inDemoInstall

raf-throttle

Package Overview
Dependencies
0
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    raf-throttle

Throttle a function by requestAnimationFrame


Version published
Maintainers
1
Install size
10.8 kB
Created

Readme

Source

raf-throttle

Throttle a function by requestAnimationFrame

npm Build Status codecov Renovate enabled

raf-throttle let you create a throttled function, which only invokes the passed function at most once per animation frame on a browser or per 1000/60 ms on Node.

Demo

Installation

npm

npm install raf-throttle --save

yarn

yarn add raf-throttle

CDN

Download the file from https://unpkg.com/raf-throttle/umd/rafThrottle.min.js,

and consume it from global as rafThrottle.

Usage

Polyfill

Since most of browsers support requestAnimationFrame by default, you can use raf-throttle directly. However, if you want to support old browsers, you will need to polyfill requestAnimationFrame by youself. One option is using raf.

Example

Avoid excessively updating the position while scrolling.

JS
import throttle from 'raf-throttle';

const throttled = throttle(updatePosition);
window.addEventListener('scroll', throttled);
jQuery
import throttle from 'raf-throttle';

$(window).on('scroll', throttle(updatePosition));
React
import React from 'react'
import throttle from 'raf-throttle'

class extends React.Component {
  onScroll = throttle(updatePosition)

  componentDidMount = () =>
    window.addEventListener('scroll', this.onScroll)

  componentWillUnmount = () =>
    window.removeEventListener('scroll', this.onScroll)

  render = () =>
    /* Your code */
}

If you think the React code is verbose and you want to move them into a higher-order component, you shoul take a look at react-dom-utils, which has done this for you.

Canceling

Cancel the trailing throttled invocation.

const throttled = throttle(foo);
throttled();
throttled.cancel(); // foo would never be invoked

API

import throttle from 'raf-throttle';
const throttled = throttle(callback)

callback is the function to be throttled by requestAnimationFrame.

throttled.cancel()

Cancel the trailing throttled invocation.

Contributing

  • ⇄ Pull requests and ★ Stars are always welcome.
  • For bugs and feature requests, please create an issue.
  • Pull requests must be accompanied by passing automated tests ($ npm test).

CHANGELOG

LICENSE

Keywords

FAQs

Last updated on 10 Dec 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc