before.js 578 B

123456789101112131415161718192021222324
  1. var slice = require('./slice')
  2. /**
  3. * 创建一个函数, 调用次数不超过 count 次之前执行回调并将所有结果记住后返回
  4. *
  5. * @param {Number} count 调用次数
  6. * @param {Function} callback 完成回调
  7. * @return {Object}
  8. */
  9. function before (count, callback, context) {
  10. var runCount = 0
  11. var rests = []
  12. context = context || this
  13. return function () {
  14. var args = arguments
  15. runCount++
  16. if (runCount < count) {
  17. rests.push(args[0])
  18. callback.apply(context, [rests].concat(slice(args)))
  19. }
  20. }
  21. }
  22. module.exports = before