You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
727 B
34 lines
727 B
2 years ago
|
"use strict";
|
||
|
|
||
|
var callable = require("es5-ext/object/valid-callable")
|
||
|
, validTimeout = require("./valid-timeout")
|
||
|
|
||
|
, apply = Function.prototype.apply;
|
||
|
|
||
|
module.exports = function (fn, timeout) {
|
||
|
var isScheduled = false, context, args, run;
|
||
|
callable(fn);
|
||
|
timeout = validTimeout(timeout);
|
||
|
run = function () {
|
||
|
var currentContext = context, currentArgs = args;
|
||
|
if (!args) {
|
||
|
isScheduled = false;
|
||
|
return;
|
||
|
}
|
||
|
context = null;
|
||
|
args = null;
|
||
|
setTimeout(run, timeout);
|
||
|
apply.call(fn, currentContext, currentArgs);
|
||
|
};
|
||
|
return function () {
|
||
|
if (isScheduled) {
|
||
|
context = this;
|
||
|
args = arguments;
|
||
|
return;
|
||
|
}
|
||
|
isScheduled = true;
|
||
|
setTimeout(run, timeout);
|
||
|
apply.call(fn, this, arguments);
|
||
|
};
|
||
|
};
|