Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. /**
  2. * Constructor abstraction of setTimeout
  3. * @constructor
  4. * @param {function} handler Function to be called when the timer triggers
  5. * @param {Number} timeout Minimum delay which the timer will triggers
  6. * @param {Array} args Arguments to be passed to the handler function
  7. * @author Victor N. wwww.victorborges.com
  8. * @date 2017-03-24
  9. */
  10. function Timeout(handler, timeout, args) {
  11. var that = this;
  12. this.created = Date.now();
  13. this.lastTriggered = null;
  14. this.timeout = timeout;
  15. this.readyState = 1; //pending
  16. this.id = window.setTimeout(function() {
  17. that.lastTriggered = Date.now();
  18. that.readyState = 4; //done
  19. delete that.id;
  20. handler.apply(that, args);
  21. }, timeout);
  22. }
  23. Timeout.prototype.cancel = function() {
  24. if (this.readyState < 4) {
  25. this.readyState = 4;
  26. window.clearTimeout(this.id);
  27. delete this.id;
  28. return true;
  29. }
  30. return false;
  31. };
  32.  
  33.  
  34.  
  35. /**
  36. * Constructor abstraction of setInterval
  37. * @constructor
  38. * @param {function} handler Function to be called when the timer triggers
  39. * @param {Number} timeout Minimum delay which the timer will triggers
  40. * @param {Array} args Arguments to be passed to the handler function
  41. * @author Victor N. wwww.victorborges.com
  42. * @date 2017-03-24
  43. */
  44. function Interval(handler, timeout, args) {
  45. var that = this;
  46. this.created = Date.now();
  47. this.previouslyTriggered = null;
  48. this.lastTriggered = null;
  49. this.n = 0;
  50. this.timeout = timeout;
  51. this.readyState = 1; //pending
  52. this.id = window.setInterval(function() {
  53. that.n++;
  54. that.previouslyTriggered = that.lastTriggered;
  55. that.lastTriggered = Date.now();
  56. that.readyState = 3; //running
  57. handler.apply(that, args);
  58. }, timeout);
  59. }
  60. Interval.prototype.cancel = function() {
  61. if (this.readyState < 4) {
  62. this.readyState = 4;
  63. window.clearInterval(this.id);
  64. delete this.id;
  65. return true;
  66. }
  67. return false;
  68. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement