_base.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.timing._base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.timing._base"] = true;
  8. dojo.provide("dojox.timing._base");
  9. dojo.experimental("dojox.timing");
  10. dojox.timing.Timer = function(/*int*/ interval){
  11. // summary: Timer object executes an "onTick()" method repeatedly at a specified interval.
  12. // repeatedly at a given interval.
  13. // interval: Interval between function calls, in milliseconds.
  14. this.timer = null;
  15. this.isRunning = false;
  16. this.interval = interval;
  17. this.onStart = null;
  18. this.onStop = null;
  19. };
  20. dojo.extend(dojox.timing.Timer, {
  21. onTick : function(){
  22. // summary: Method called every time the interval passes. Override to do something useful.
  23. },
  24. setInterval : function(interval){
  25. // summary: Reset the interval of a timer, whether running or not.
  26. // interval: New interval, in milliseconds.
  27. if (this.isRunning){
  28. window.clearInterval(this.timer);
  29. }
  30. this.interval = interval;
  31. if (this.isRunning){
  32. this.timer = window.setInterval(dojo.hitch(this, "onTick"), this.interval);
  33. }
  34. },
  35. start : function(){
  36. // summary: Start the timer ticking.
  37. // description: Calls the "onStart()" handler, if defined.
  38. // Note that the onTick() function is not called right away,
  39. // only after first interval passes.
  40. if (typeof this.onStart == "function"){
  41. this.onStart();
  42. }
  43. this.isRunning = true;
  44. this.timer = window.setInterval(dojo.hitch(this, "onTick"), this.interval);
  45. },
  46. stop : function(){
  47. // summary: Stop the timer.
  48. // description: Calls the "onStop()" handler, if defined.
  49. if (typeof this.onStop == "function"){
  50. this.onStop();
  51. }
  52. this.isRunning = false;
  53. window.clearInterval(this.timer);
  54. }
  55. });
  56. }