throttle-wrapper
v1.0.5
Published
Function ignoring call child function more often than ms
Downloads
10
Readme
throttle-wrapper
Function ignoring call child function more often than ms
Table of Contents
Quick start
Install
We support all platforms.
npm
For module bundlers such as Webpack or Browserify.
npm i throttle-wrapper
Include with <script>
- Download lib
- Add script to html
<script src="throttle-wrapper.js"></script>
CDN
Recommended for learning purposes, you can use the latest version:
<script src="https://cdn.jsdelivr.net/npm/throttle-wrapper/dist/lib/throttle-wrapper.js"></script>
Recommended for production for avoiding unexpected breakage from newer versions:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/lib/throttle-wrapper.js"></script>
Initialization
ES6
throttle-wrapper as an ES6 module.
import throttle from 'throttle-wrapper';
let result = [],
fn = (number) => result.push(number),
callFnWithThrottle = throttle(fn, 500)
callFnWithThrottle(1)
setTimeout(() => callFnWithThrottle(2.1),100)
setTimeout(() => callFnWithThrottle(2.2),150)
setTimeout(() => callFnWithThrottle(2.3),180)
setTimeout(() => callFnWithThrottle(3),200)
setTimeout(() => console.log(result),300) //[1, 3]
Node
throttle-wrapper as a Node.js module
const throttle = require('throttle-wrapper');
let result = [],
fn = (number) => result.push(number),
callFnWithThrottle = throttle(fn, 500)
callFnWithThrottle(1)
setTimeout(() => callFnWithThrottle(2.1),100)
setTimeout(() => callFnWithThrottle(2.2),150)
setTimeout(() => callFnWithThrottle(2.3),180)
setTimeout(() => callFnWithThrottle(3),200)
setTimeout(() => console.log(result),300) //[1, 3]
Browser
Exports a global variable called throttle
. Use it like this
Connect to html file <script src="https://cdn.jsdelivr.net/npm/throttle-wrapper/dist/lib/throttle-wrapper.js" ></script>
<script>
var result = [],
fn = (number) => result.push(number),
callFnWithThrottle = throttle(fn, 500)
callFnWithThrottle(1)
setTimeout(() => callFnWithThrottle(2.1),100)
setTimeout(() => callFnWithThrottle(2.2),150)
setTimeout(() => callFnWithThrottle(2.3),180)
setTimeout(() => callFnWithThrottle(3),200)
setTimeout(() => console.log(result),300) //[1, 3]
</script>
AMD
throttle-wrapper as an AMD module. Use with Require.js, System.js, and so on.
- Download lib
- Connect to your module loader
requirejs(['throttle-wrapper'], function(throttle) {
var result = [],
fn = (number) => result.push(number),
callFnWithThrottle = throttle(fn, 500)
callFnWithThrottle(1)
setTimeout(() => callFnWithThrottle(2.1),100)
setTimeout(() => callFnWithThrottle(2.2),150)
setTimeout(() => callFnWithThrottle(2.3),180)
setTimeout(() => callFnWithThrottle(3),200)
setTimeout(() => console.log(result),300) //[1, 3]
});
Methods
throttle
Function ignoring call child function more often than ms
Params
fn
- Type:
function
- Description: function that will be called after ms
- Type:
ms
- Type:
number
- Description: time out, after which fn will call
- Type:
Returns
function
Example
let result = [],
fn = (number) => result.push(number),
callFnWithThrottle = throttle(fn, 500)
callFnWithThrottle(1)
setTimeout(() => callFnWithThrottle(2.1),100)
setTimeout(() => callFnWithThrottle(2.2),150)
setTimeout(() => callFnWithThrottle(2.3),180)
setTimeout(() => callFnWithThrottle(3),200)
setTimeout(() => console.log(result),300) // => [1, 3]
Author
webster6667