Jquery.timer

A jquery timer

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/441621/1029050/Jquerytimer.js

  1. // ==UserScript==
  2. // @name SimpleFilter
  3. // @namespace ckylin-script-lib-timer
  4. // @version 1.2
  5. // @match http://*
  6. // @match https://*
  7. // @author Jason Chavannes
  8. // @license GPLv3 License
  9. // @grant none
  10. // @description A jquery timer
  11. // @lanuage script
  12. // ==/UserScript==
  13. ;(function($) {
  14. $.timer = function(func, time, autostart) {
  15. this.set = function(func, time, autostart) {
  16. this.init = true;
  17. if(typeof func == 'object') {
  18. var paramList = ['autostart', 'time'];
  19. for(var arg in paramList) {if(func[paramList[arg]] != undefined) {eval(paramList[arg] + " = func[paramList[arg]]");}};
  20. func = func.action;
  21. }
  22. if(typeof func == 'function') {this.action = func;}
  23. if(!isNaN(time)) {this.intervalTime = time;}
  24. if(autostart && !this.isActive) {
  25. this.isActive = true;
  26. this.setTimer();
  27. }
  28. return this;
  29. };
  30. this.once = function(time) {
  31. var timer = this;
  32. if(isNaN(time)) {time = 0;}
  33. window.setTimeout(function() {timer.action();}, time);
  34. return this;
  35. };
  36. this.play = function(reset) {
  37. if(!this.isActive) {
  38. if(reset) {this.setTimer();}
  39. else {this.setTimer(this.remaining);}
  40. this.isActive = true;
  41. }
  42. return this;
  43. };
  44. this.pause = function() {
  45. if(this.isActive) {
  46. this.isActive = false;
  47. this.remaining -= new Date() - this.last;
  48. this.clearTimer();
  49. }
  50. return this;
  51. };
  52. this.stop = function() {
  53. this.isActive = false;
  54. this.remaining = this.intervalTime;
  55. this.clearTimer();
  56. return this;
  57. };
  58. this.toggle = function(reset) {
  59. if(this.isActive) {this.pause();}
  60. else if(reset) {this.play(true);}
  61. else {this.play();}
  62. return this;
  63. };
  64. this.reset = function() {
  65. this.isActive = false;
  66. this.play(true);
  67. return this;
  68. };
  69. this.clearTimer = function() {
  70. window.clearTimeout(this.timeoutObject);
  71. };
  72. this.setTimer = function(time) {
  73. var timer = this;
  74. if(typeof this.action != 'function') {return;}
  75. if(isNaN(time)) {time = this.intervalTime;}
  76. this.remaining = time;
  77. this.last = new Date();
  78. this.clearTimer();
  79. this.timeoutObject = window.setTimeout(function() {timer.go();}, time);
  80. };
  81. this.go = function() {
  82. if(this.isActive) {
  83. this.action();
  84. this.setTimer();
  85. }
  86. };
  87. if(this.init) {
  88. return new $.timer(func, time, autostart);
  89. } else {
  90. this.set(func, time, autostart);
  91. return this;
  92. }
  93. };
  94. })(jQuery);