just-throttle
Advanced tools
Comparing version 1.1.0 to 2.0.0
19
index.js
module.exports = throttle; | ||
function throttle(fn, interval, callFirst) { | ||
function throttle(fn, interval, options) { | ||
var wait = false; | ||
var callNow = false; | ||
var leading = (options && options.leading); | ||
var trailing = (options && options.trailing); | ||
if (leading == null) { | ||
leading = true; // default | ||
} | ||
if (trailing == null) { | ||
trailing = !leading; //default | ||
} | ||
if (leading == true) { | ||
trailing = false; // forced because there should be invocation per call | ||
} | ||
return function() { | ||
callNow = callFirst && !wait; | ||
var callNow = leading && !wait; | ||
var context = this; | ||
@@ -14,3 +25,3 @@ var args = arguments; | ||
wait = false; | ||
if (!callFirst) { | ||
if (trailing) { | ||
return fn.apply(context, args); | ||
@@ -17,0 +28,0 @@ } |
{ | ||
"name": "just-throttle", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "return a throttled function", | ||
@@ -21,2 +21,2 @@ "main": "index.js", | ||
} | ||
} | ||
} |
@@ -11,9 +11,12 @@ ## just-throttle | ||
const fn1 = throttle(() => console.log('hello'), 500, true); | ||
const fn1 = throttle(() => console.log('hello'), 500, {leading: true}); | ||
setInterval(fn1, 400); | ||
// logs 'hello' immediately and then every 500ms | ||
const fn2 = throttle(() => console.log('hello'), 500); | ||
const fn2 = throttle(() => console.log('hello'), 500, {trailing: true}); | ||
setInterval(fn2, 400); | ||
// logs 'hello' after 500ms and then every 500ms | ||
const fn2 = throttle(() => console.log('hello'), 500, {leading: true, trailing: true}); | ||
// forces trailing to false | ||
``` |
3006
33
22