debounce.js 923 B

12345678910111213141516171819202122232425262728293031
  1. define([], function(){
  2. // module:
  3. // dojo/debounce
  4. // summary:
  5. // This module provide a debouncer
  6. return function(cb, wait){
  7. // summary:
  8. // Create a function that will only execute after `wait` milliseconds
  9. // description:
  10. // Create a function that will only execute after `wait` milliseconds
  11. // of repeated execution. Useful for delaying some event action slightly to allow
  12. // for rapidly-firing events such as window.resize, node.mousemove and so on.
  13. // cb: Function
  14. // A callback to fire. Like hitch() and partial(), arguments passed to the
  15. // returned function curry along to the original callback.
  16. // wait: Integer
  17. // Time to spend caching executions before actually executing.
  18. var timer;
  19. return function(){
  20. if(timer){
  21. clearTimeout(timer);
  22. }
  23. var self = this;
  24. var a = arguments;
  25. timer = setTimeout(function(){
  26. cb.apply(self, a);
  27. }, wait);
  28. };
  29. };
  30. });