Subversion Repositories HomeAutomation

Rev

Rev 1042 | Blame | Compare with Previous | 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.         //log("Interval:" + id + "> Triggered!\n");
  27.         Interval.myIntervals[id].triggerCallback();
  28.     }
  29.     else
  30.     {
  31.         log("Interval:" + id + "> Encountered a thread that have no javascript Interval object. This is not supposed to be possible...\n");
  32.     }
  33. }
  34.  
  35. Interval.prototype.triggerCallback = function()
  36. {
  37.     this.myCallback();
  38. }
  39.  
  40. Interval.prototype.setCallback = function(callback)
  41. {
  42.     this.myCallback = callback;
  43. }
  44.  
  45. Interval.prototype.start = function()
  46. {
  47.     if (!this.myIsRunning)
  48.     {
  49.         this.myIsRunning = true;
  50.         this.myId = startIntervalThread(this.myTimeout, this.mySingle)
  51.         Interval.myIntervals[this.myId] = this;
  52.     }
  53. }
  54.  
  55. Interval.prototype.stop = function()
  56. {
  57.     if (this.myIsRunning)
  58.     {
  59.         this.myIsRunning = false;
  60.        
  61.         if (!stopIntervalThread(this.myId))
  62.         {
  63.             log("Interval:" + this.myId + "> Interval thread seems to have already been stopped.\n");
  64.         }
  65.        
  66.         delete Interval.myIntervals[this.myId];
  67.         this.myId = null;
  68.     }
  69. }
  70.