delay.js 506 B

123456789101112131415161718192021
  1. "use strict";
  2. var callable = require("es5-ext/object/valid-callable")
  3. , nextTick = require("next-tick")
  4. , validTimeout = require("./valid-timeout");
  5. var apply = Function.prototype.apply;
  6. module.exports = function (fn/*, timeout*/) {
  7. var delay, timeout = arguments[1];
  8. callable(fn);
  9. if (timeout === undefined) {
  10. delay = nextTick;
  11. } else {
  12. timeout = validTimeout(timeout);
  13. delay = setTimeout;
  14. }
  15. return function () {
  16. return delay(apply.bind(fn, this, arguments), timeout);
  17. };
  18. };