jquery.timer.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * jquery.timer.js
  3. *
  4. * Copyright (c) 2011 Jason Chavannes <jason.chavannes@gmail.com>
  5. *
  6. * http://jchavannes.com/jquery-timer
  7. *
  8. * Permission is hereby granted, free of charge, to any person
  9. * obtaining a copy of this software and associated documentation
  10. * files (the "Software"), to deal in the Software without
  11. * restriction, including without limitation the rights to use, copy,
  12. * modify, merge, publish, distribute, sublicense, and/or sell copies
  13. * of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  23. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  24. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  25. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. * SOFTWARE.
  27. */
  28. ;(function($) {
  29. $.timer = function(func, time, autostart) {
  30. this.set = function(func, time, autostart) {
  31. this.init = true;
  32. if(typeof func == 'object') {
  33. var paramList = ['autostart', 'time'];
  34. for(var arg in paramList) {if(func[paramList[arg]] != undefined) {eval(paramList[arg] + " = func[paramList[arg]]");}};
  35. func = func.action;
  36. }
  37. if(typeof func == 'function') {this.action = func;}
  38. if(!isNaN(time)) {this.intervalTime = time;}
  39. if(autostart && !this.isActive) {
  40. this.isActive = true;
  41. this.setTimer();
  42. }
  43. return this;
  44. };
  45. this.once = function(time) {
  46. var timer = this;
  47. if(isNaN(time)) {time = 0;}
  48. window.setTimeout(function() {timer.action();}, time);
  49. return this;
  50. };
  51. this.play = function(reset) {
  52. if(!this.isActive) {
  53. if(reset) {this.setTimer();}
  54. else {this.setTimer(this.remaining);}
  55. this.isActive = true;
  56. }
  57. return this;
  58. };
  59. this.pause = function() {
  60. if(this.isActive) {
  61. this.isActive = false;
  62. this.remaining -= new Date() - this.last;
  63. this.clearTimer();
  64. }
  65. return this;
  66. };
  67. this.stop = function() {
  68. this.isActive = false;
  69. this.remaining = this.intervalTime;
  70. this.clearTimer();
  71. return this;
  72. };
  73. this.toggle = function(reset) {
  74. if(this.isActive) {this.pause();}
  75. else if(reset) {this.play(true);}
  76. else {this.play();}
  77. return this;
  78. };
  79. this.reset = function() {
  80. this.isActive = false;
  81. this.play(true);
  82. return this;
  83. };
  84. this.clearTimer = function() {
  85. window.clearTimeout(this.timeoutObject);
  86. };
  87. this.setTimer = function(time) {
  88. var timer = this;
  89. if(typeof this.action != 'function') {return;}
  90. if(isNaN(time)) {time = this.intervalTime;}
  91. this.remaining = time;
  92. this.last = new Date();
  93. this.clearTimer();
  94. this.timeoutObject = window.setTimeout(function() {timer.go();}, time);
  95. };
  96. this.go = function() {
  97. if(this.isActive) {
  98. this.action();
  99. this.setTimer();
  100. }
  101. };
  102. if(this.init) {
  103. return new $.timer(func, time, autostart);
  104. } else {
  105. this.set(func, time, autostart);
  106. return this;
  107. }
  108. };
  109. })(jQuery);