X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=node_modules%2Fthrottleit%2Findex.js;fp=node_modules%2Fthrottleit%2Findex.js;h=1cc3ad61079d424289b28f14b48b3f70c7a73847;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/node_modules/throttleit/index.js b/node_modules/throttleit/index.js new file mode 100644 index 000000000..1cc3ad610 --- /dev/null +++ b/node_modules/throttleit/index.js @@ -0,0 +1,32 @@ +module.exports = throttle; + +/** + * Returns a new function that, when invoked, invokes `func` at most once per `wait` milliseconds. + * + * @param {Function} func Function to wrap. + * @param {Number} wait Number of milliseconds that must elapse between `func` invocations. + * @return {Function} A new function that wraps the `func` function passed in. + */ + +function throttle (func, wait) { + var ctx, args, rtn, timeoutID; // caching + var last = 0; + + return function throttled () { + ctx = this; + args = arguments; + var delta = new Date() - last; + if (!timeoutID) + if (delta >= wait) call(); + else timeoutID = setTimeout(call, wait - delta); + return rtn; + }; + + function call () { + timeoutID = 0; + last = +new Date(); + rtn = func.apply(ctx, args); + ctx = null; + args = null; + } +}