Function ignoring call child function more often than ms
We support all platforms.
For module bundlers such as Webpack or Browserify.
npm i throttle-wrapper
- Download lib
- Add script to html
<script src="throttle-wrapper.js"></script>
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/throttle-wrapper@0.0.0/dist/lib/throttle-wrapper.js"></script>
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]
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]
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>
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]
});
Function ignoring call child function more often than ms
fn
- Type:
function
- Description: function that will be called after ms
- Type:
ms
- Type:
number
- Description: time out, after which fn will call
- Type:
function
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]
webster6667