throttle.js 804 B

12345678910111213141516171819202122232425262728293031
  1. define([], function(){
  2. // module:
  3. // dojo/throttle
  4. // summary:
  5. // This module provide a throttler
  6. return function(cb, wait){
  7. // summary:
  8. // Create a function that will only execute once per `wait` periods.
  9. // description:
  10. // Create a function that will only execute once per `wait` periods
  11. // from last execution when called repeatedly. Useful for preventing excessive
  12. // calculations in rapidly firing events, such as window.resize, node.mousemove
  13. // and so on.
  14. // cb: Function
  15. // The callback to fire.
  16. // wait: Integer
  17. // time to delay before allowing cb to call again.
  18. var canrun = true;
  19. return function(){
  20. if(!canrun){
  21. return;
  22. }
  23. canrun = false;
  24. cb.apply(this, arguments);
  25. setTimeout(function(){
  26. canrun = true;
  27. }, wait);
  28. };
  29. };
  30. });