Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

1234567891011121314151617181920212223242526272829303132333435363738
  1. define(['./restArguments', './delay'], function (restArguments, delay) {
  2. // When a sequence of calls of the returned function ends, the argument
  3. // function is triggered. The end of a sequence is defined by the `wait`
  4. // parameter. If `immediate` is passed, the argument function will be
  5. // triggered at the beginning of the sequence instead of at the end.
  6. function debounce(func, wait, immediate) {
  7. var timeout, result;
  8. var later = function(context, args) {
  9. timeout = null;
  10. if (args) result = func.apply(context, args);
  11. };
  12. var debounced = restArguments(function(args) {
  13. if (timeout) clearTimeout(timeout);
  14. if (immediate) {
  15. var callNow = !timeout;
  16. timeout = setTimeout(later, wait);
  17. if (callNow) result = func.apply(this, args);
  18. } else {
  19. timeout = delay(later, wait, this, args);
  20. }
  21. return result;
  22. });
  23. debounced.cancel = function() {
  24. clearTimeout(timeout);
  25. timeout = null;
  26. };
  27. return debounced;
  28. }
  29. return debounce;
  30. });