home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / attention_hog.swf / scripts / __Packages / com / leadpipe / attentionhog / Timer.as < prev   
Encoding:
Text File  |  2008-09-22  |  2.5 KB  |  108 lines

  1. class com.leadpipe.attentionhog.Timer
  2. {
  3.    var object;
  4.    var methodName;
  5.    var minTime;
  6.    var maxTime;
  7.    var time;
  8.    var timeStart;
  9.    var id;
  10.    var isPaused;
  11.    var timeRemaining;
  12.    static var instanceList = new Array();
  13.    function Timer(o, m, min, max)
  14.    {
  15.       com.leadpipe.attentionhog.Timer.instanceList.push(this);
  16.       this.object = o;
  17.       this.methodName = m;
  18.       this.minTime = min;
  19.       this.maxTime = max;
  20.    }
  21.    function start()
  22.    {
  23.       this.time = this.randomInt(this.minTime,this.maxTime);
  24.       this.timeStart = getTimer();
  25.       if(this.id != null)
  26.       {
  27.          clearTimeout(this.id);
  28.       }
  29.       this.id = setTimeout(this,"doMethod",this.time);
  30.    }
  31.    function doMethod()
  32.    {
  33.       this.object[this.methodName]();
  34.       this.start();
  35.    }
  36.    function pause(p)
  37.    {
  38.       this.isPaused = p;
  39.       if(this.isPaused)
  40.       {
  41.          if(this.id != null)
  42.          {
  43.             clearTimeout(this.id);
  44.          }
  45.          this.timeRemaining = this.time - (getTimer() - this.timeStart);
  46.       }
  47.       else
  48.       {
  49.          this.timeStart = getTimer();
  50.          if(this.id != null)
  51.          {
  52.             clearTimeout(this.id);
  53.          }
  54.          this.id = setTimeout(this,"doMethod",this.timeRemaining);
  55.       }
  56.    }
  57.    function stop()
  58.    {
  59.       if(this.id != null)
  60.       {
  61.          clearTimeout(this.id);
  62.       }
  63.    }
  64.    function clear()
  65.    {
  66.       if(this.id != null)
  67.       {
  68.          clearTimeout(this.id);
  69.       }
  70.    }
  71.    function destroy()
  72.    {
  73.       this.clear();
  74.       for(var _loc2_ in com.leadpipe.attentionhog.Timer.instanceList)
  75.       {
  76.          if(com.leadpipe.attentionhog.Timer.instanceList[_loc2_] == this)
  77.          {
  78.             com.leadpipe.attentionhog.Timer.instanceList.splice(_loc2_,1);
  79.             break;
  80.          }
  81.       }
  82.    }
  83.    function ┬ºset┬º(min, max)
  84.    {
  85.       this.minTime = min;
  86.       this.maxTime = max;
  87.    }
  88.    function randomInt(min, max)
  89.    {
  90.       return Math.floor(Math.random() * (max - min + 1)) + min;
  91.    }
  92.    static function destroyAll()
  93.    {
  94.       for(var _loc1_ in com.leadpipe.attentionhog.Timer.instanceList)
  95.       {
  96.          com.leadpipe.attentionhog.Timer.instanceList[_loc1_].destroy();
  97.       }
  98.       com.leadpipe.attentionhog.Timer.instanceList = new Array();
  99.    }
  100.    static function pauseAll(p)
  101.    {
  102.       for(var _loc2_ in com.leadpipe.attentionhog.Timer.instanceList)
  103.       {
  104.          com.leadpipe.attentionhog.Timer.instanceList[_loc2_].pause(p);
  105.       }
  106.    }
  107. }
  108.