Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Blame | Last modification | View Log | SVN | RSS feed

  1.  
  2. function Interval(callback, timeout, single)
  3. {
  4.     if (single == null)
  5.     {
  6.         single = false;
  7.     }
  8.    
  9.     this.myCallback = callback;
  10.     this.myTimeout = timeout;
  11.     this.mySingle = single;
  12.     this.myIsRunning = false;
  13. }
  14.  
  15. Interval.myIntervals = new Array();
  16. Interval.prototype.myIsRunning = null;
  17. Interval.prototype.myId = null;
  18. Interval.prototype.myTimeout = null;
  19. Interval.prototype.mySingle = null;
  20. Interval.prototype.myCallback = null;
  21.  
  22. Interval.triggerIntervalCallback = function(id)
  23. {
  24.     if (Interval.myIntervals[id])
  25.     {
  26.         if (Interval.myIntervals[id].myIsRunning)
  27.         {
  28.             //log("Interval:" + id + "> Triggered!\n");
  29.             Interval.myIntervals[id].triggerCallback();
  30.         }
  31.     }
  32.     else
  33.     {
  34.         log("Interval:" + id + "> Encountered a thread that have no javascript Interval object. This is not supposed to be possible...\n");
  35.     }
  36. }
  37.  
  38. Interval.prototype.triggerCallback = function()
  39. {
  40.     if (this.mySingle)
  41.     {
  42.         this.myIsRunning = false;
  43.     }
  44.    
  45.     this.myCallback();
  46. }
  47.  
  48. Interval.prototype.setCallback = function(callback)
  49. {
  50.     this.myCallback = callback;
  51. }
  52.  
  53. Interval.prototype.setTimeout = function(timeout)
  54. {
  55.     this.stop();
  56.     this.myTimeout = timeout;
  57.     this.start();
  58. }
  59.  
  60. Interval.prototype.reset = function()
  61. {
  62.     this.stop();
  63.     this.start();
  64. }
  65.  
  66. Interval.prototype.start = function()
  67. {
  68.     if (!this.myIsRunning)
  69.     {
  70.         this.myIsRunning = true;
  71.        
  72.         if (this.mySingle)
  73.         {
  74.             this.myId = SetTimeout(Interval.triggerIntervalCallback, this.myTimeout);
  75.         }
  76.         else
  77.         {
  78.             this.myId = SetInterval(Interval.triggerIntervalCallback, this.myTimeout);
  79.         }
  80.        
  81.         Interval.myIntervals[this.myId] = this;
  82.     }
  83. }
  84.  
  85. Interval.prototype.stop = function()
  86. {
  87.     if (this.myIsRunning)
  88.     {
  89.         //log("Interval:" + this.myId + "> Interval thread stop.\n");
  90.        
  91.         this.myIsRunning = false;
  92.        
  93.         ClearInterval(this.myId);
  94.        
  95.         delete Interval.myIntervals[this.myId];
  96.         this.myId = null;
  97.     }
  98. }
  99.