Debian Codegraph @

timers-ext/throttle.js

raw
1 "use strict";
2
3 var callable = require("es5-ext/object/valid-callable")
4 , validTimeout = require("./valid-timeout")
5 , apply = Function.prototype.apply;
6
7 module.exports = function (fn, timeout) {
8 var isScheduled = false, context, args, run;
9 callable(fn);
10 timeout = validTimeout(timeout);
11 run = function () {
12 var currentContext = context, currentArgs = args;
13 if (!args) {
14 isScheduled = false;
15 return;
16 }
17 context = null;
18 args = null;
19 setTimeout(run, timeout);
20 apply.call(fn, currentContext, currentArgs);
21 };
22 return function () {
23 if (isScheduled) {
24 context = this;
25 args = arguments;
26 return;
27 }
28 isScheduled = true;
29 setTimeout(run, timeout);
30 apply.call(fn, this, arguments);
31 };
32 };