Subversion Repositories HomeAutomation

Rev

Rev 1042 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

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