Subversion Repositories HomeAutomation

Rev

Rev 973 | Rev 1042 | Go to most recent revision | 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.         Interval.myIntervals[id].triggerCallback();
  20.     }
  21.     else
  22.     {
  23.         log("Interval:" + id + "> Encountered a thread that have no javascript Interval object. This is not supposed to be possible, report this.\n");
  24.        
  25.         if (!stopIntervalThread(id))
  26.         {
  27.             log("Interval:" + id + "> Interval thread could not be stopped.\n");
  28.         }
  29.     }
  30. }
  31.  
  32. Interval.prototype.triggerCallback = function()
  33. {
  34.     this.myCallback();
  35. }
  36.  
  37. Interval.prototype.setTimeout = function(timeout)
  38. {
  39.     var isRunning = this.myIsRunning;
  40.     if (isRunning)
  41.     {
  42.         this.stop();
  43.     }
  44.  
  45.     this.myTimeout = timeout;
  46.    
  47.     if (isRunning)
  48.     {
  49.         this.start();
  50.     }
  51. }
  52.  
  53. Interval.prototype.setCallback = function(callback)
  54. {
  55.     this.myCallback = callback;
  56. }
  57.  
  58. Interval.prototype.start = function()
  59. {
  60.     if (!this.myIsRunning)
  61.     {
  62.         this.myIsRunning = true;
  63.         this.myId = startIntervalThread(this.myTimeout)
  64.         Interval.myIntervals[this.myId] = this;
  65.     }
  66. }
  67.  
  68. Interval.prototype.stop = function()
  69. {
  70.     if (this.myIsRunning)
  71.     {
  72.         this.myIsRunning = false;
  73.        
  74.         if (!stopIntervalThread(this.myId))
  75.         {
  76.             log("Interval:" + this.myId + "> Interval thread could not be stopped.\n");
  77.         }
  78.        
  79.         delete Interval.myIntervals[this.myId];
  80.         this.myId = null;
  81.     }
  82. }
  83.