awaitDelay.js 812 B

12345678910111213141516171819202122
  1. var now = require('../time/now');
  2. var timeout = require('./timeout');
  3. var append = require('../array/append');
  4. /**
  5. * Ensure a minimum delay for callbacks
  6. */
  7. function awaitDelay( callback, delay ){
  8. var baseTime = now() + delay;
  9. return function() {
  10. // ensure all browsers will execute it asynchronously (avoid hard
  11. // to catch errors) not using "0" because of old browsers and also
  12. // since new browsers increase the value to be at least "4"
  13. // http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#dom-windowtimers-settimeout
  14. var ms = Math.max(baseTime - now(), 4);
  15. return timeout.apply(this, append([callback, ms, this], arguments));
  16. };
  17. }
  18. module.exports = awaitDelay;