timer.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*----------------------------------------------------------------------------\
  2. | Timer Class |
  3. |-----------------------------------------------------------------------------|
  4. | Created by Erik Arvidsson |
  5. | (http://webfx.eae.net/contact.html#erik) |
  6. | For WebFX (http://webfx.eae.net/) |
  7. |-----------------------------------------------------------------------------|
  8. | Object Oriented Encapsulation of setTimeout fires ontimer when the timer |
  9. | is triggered. Does not work in IE 5.00 |
  10. |-----------------------------------------------------------------------------|
  11. | Copyright (c) 2002, 2006 Erik Arvidsson |
  12. |-----------------------------------------------------------------------------|
  13. | Licensed under the Apache License, Version 2.0 (the "License"); you may not |
  14. | use this file except in compliance with the License. You may obtain a copy |
  15. | of the License at http://www.apache.org/licenses/LICENSE-2.0 |
  16. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  17. | Unless required by applicable law or agreed to in writing, software |
  18. | distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
  19. | WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
  20. | License for the specific language governing permissions and limitations |
  21. | under the License. |
  22. |-----------------------------------------------------------------------------|
  23. | 2002-10-14 | Original version released |
  24. | 2006-05-28 | Changed license to Apache Software License 2.0. |
  25. |-----------------------------------------------------------------------------|
  26. | Created 2002-10-14 | All changes are in the log above. | Updated 2006-05-28 |
  27. \----------------------------------------------------------------------------*/
  28. function Timer(nPauseTime) {
  29. this._pauseTime = typeof nPauseTime == "undefined" ? 1000 : nPauseTime;
  30. this._timer = null;
  31. this._isStarted = false;
  32. }
  33. Timer.prototype.start = function () {
  34. if (this.isStarted())
  35. this.stop();
  36. var oThis = this;
  37. this._timer = window.setTimeout(function () {
  38. if (typeof oThis.ontimer == "function")
  39. oThis.ontimer();
  40. }, this._pauseTime);
  41. this._isStarted = false;
  42. };
  43. Timer.prototype.stop = function () {
  44. if (this._timer != null)
  45. window.clearTimeout(this._timer);
  46. this._isStarted = false;
  47. };
  48. Timer.prototype.isStarted = function () {
  49. return this._isStarted;
  50. };
  51. Timer.prototype.getPauseTime = function () {
  52. return this._pauseTime;
  53. };
  54. Timer.prototype.setPauseTime = function (nPauseTime) {
  55. this._pauseTime = nPauseTime;
  56. };