_base.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. define("dojox/timing/_base", ["dojo/_base/kernel", "dojo/_base/lang"], function(dojo){
  2. dojo.experimental("dojox.timing");
  3. dojo.getObject("timing", true, dojox);
  4. dojox.timing.Timer = function(/*int*/ interval){
  5. // summary: Timer object executes an "onTick()" method repeatedly at a specified interval.
  6. // repeatedly at a given interval.
  7. // interval: Interval between function calls, in milliseconds.
  8. this.timer = null;
  9. this.isRunning = false;
  10. this.interval = interval;
  11. this.onStart = null;
  12. this.onStop = null;
  13. };
  14. dojo.extend(dojox.timing.Timer, {
  15. onTick: function(){
  16. // summary: Method called every time the interval passes. Override to do something useful.
  17. },
  18. setInterval: function(interval){
  19. // summary: Reset the interval of a timer, whether running or not.
  20. // interval: New interval, in milliseconds.
  21. if (this.isRunning){
  22. window.clearInterval(this.timer);
  23. }
  24. this.interval = interval;
  25. if (this.isRunning){
  26. this.timer = window.setInterval(dojo.hitch(this, "onTick"), this.interval);
  27. }
  28. },
  29. start: function(){
  30. // summary: Start the timer ticking.
  31. // description: Calls the "onStart()" handler, if defined.
  32. // Note that the onTick() function is not called right away,
  33. // only after first interval passes.
  34. if (typeof this.onStart == "function"){
  35. this.onStart();
  36. }
  37. this.isRunning = true;
  38. this.timer = window.setInterval(dojo.hitch(this, "onTick"), this.interval);
  39. },
  40. stop: function(){
  41. // summary: Stop the timer.
  42. // description: Calls the "onStop()" handler, if defined.
  43. if (typeof this.onStop == "function"){
  44. this.onStop();
  45. }
  46. this.isRunning = false;
  47. window.clearInterval(this.timer);
  48. }
  49. });
  50. return dojox.timing;
  51. });