Subversion Repositories HomeAutomation

Rev

Rev 971 | Rev 974 | 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.     this.myIsRunning = true;
  61.     this.myId = startIntervalThread(this.myTimeout)
  62.     Interval.myIntervals[this.myId] = this;
  63. }
  64.  
  65. Interval.prototype.stop = function()
  66. {
  67.     this.myIsRunning = false;
  68.    
  69.     if (!stopIntervalThread(this.myId))
  70.     {
  71.         log("Interval:" + this.myId + "> Interval thread could not be stopped.\n");
  72.     }
  73.    
  74.     delete Interval.myIntervals[this.myId];
  75.     this.myId = null;
  76. }
  77.