Code Monkey home page Code Monkey logo

raf-throttle's Introduction

raf-throttle

Throttle a function by requestAnimationFrame

npm Travis Codecov Code Climate

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.

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).

raf-throttle's People

Contributors

wuct avatar greenkeeperio-bot avatar greenkeeper[bot] avatar ivikash avatar bfred-it avatar

Watchers

James Cloos avatar Abhilash Srivastava 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.