Source for file ATimer.php

Documentation is available at ATimer.php

  1. <?php
  2. /**
  3.  * ATimer.php
  4.  * File for ATimer class - the Alia Javascript Timer
  5.  * 
  6.  * @author Jordan CM Wambaugh <jordan@wambaugh.org>
  7.  * @package Alia
  8.  * 
  9.  */
  10.  
  11.  
  12. /**
  13.  * Alia Javascript timer
  14.  * 
  15.  * ATimer is a javascript timer for Alia applications.
  16.  * If the timer is set to singleshot, the timer will run once, and once $interval has passed, the timeout signal will be emitted.
  17.  * If the timer's singleshot is set to false, the timer will continue to run every $interval milliseconds.
  18.  * 
  19.  * Note that this is a javascript timer: the timer will not actually start until the data is sent back to the client.
  20.  * @author Jordan CM Wambaugh <jordan@wambaugh.org>
  21.  * @package Alia
  22.  * @signal timeout Emitted every time the timer times out (every interval)
  23.  */
  24. class ATimer extends AObject{
  25.     
  26.     /**
  27.      * Milliseconds till timeout is emitted
  28.      * @var int 
  29.      * @see setInterval()
  30.      */
  31.     private $interval;
  32.     
  33.     /**
  34.      * Whether the timer is singleshot or not.
  35.      * singleshot timers only run once.
  36.      * If this is set to false, the timer will continue to emit the timeout signal continuosly every $interval number of milliseconds.
  37.      *
  38.      * @var bool 
  39.      */
  40.     private $singleshot = false;
  41.     
  42.     /**
  43.      * Whether the timer is currently running
  44.      *
  45.      * @var bool 
  46.      */
  47.     private $active=false;
  48.     
  49.     public function __construct($interval=null,$singleshot false){
  50.         parent::__construct();
  51.         $this->defineSignal('timeout');
  52.  
  53.         
  54.         $this->setInterval$interval);
  55.         $this->setSingleShot($singleshot);
  56.         
  57.         //define our timer variable in javascript
  58.         Alia::sendJScript('var '.$this->getObjectID()."Timer = new function(){this.timer = null;this.singleshot=".($this->singleshot==true?'true':'false').";}" );
  59.         Alia::connect($this,'timeout',null,null,'if('.$this->getObjectID().'Timer.singleshot==false){'.$this->getObjectID()."Timer.timer = setTimeout(\"".AJScript::emit('timeout',$this)."\",{$this->interval});}");
  60.         
  61.     }
  62.     
  63.     
  64.     /**
  65.      * sets the intervals between timeouts (in milliseconds)
  66.      *
  67.      * @param unknown_type $interval
  68.      * @see $interval
  69.      */
  70.     public function setInterval($interval){
  71.         $this->interval = $interval;
  72.     }
  73.     
  74.     /**
  75.      * Sets whether the timer should repeat or just run once.
  76.      *
  77.      * @param bool $val true if only runs once, false to run repeatedly
  78.      */
  79.     public function setSingleShot($val){
  80.         $this->singleshot = (bool)$val;
  81.     }
  82.     
  83.     /**
  84.      * stops the timer
  85.      *
  86.      */
  87.     public function stop(){
  88.         if($this->active == false){
  89.             return ;
  90.         }
  91.         $this->active=false;
  92.         Alia::sendJScript('clearTimeout('.$this->getObjectID()."Timer.timer);");
  93.     }
  94.     
  95.     /**
  96.      * Starts the timer
  97.      *
  98.      */
  99.     public function start(){
  100.         if($this->interval==null){
  101.             throw new Exception("Cannot start timer with null or 0 interval.");
  102.         }
  103.         Alia::sendJScript($this->getObjectID()."Timer.timer = setTimeout(\"".AJScript::emit('timeout',$this)."\",{$this->interval});");
  104.         $this->active=true;
  105.     }
  106.     
  107.     
  108.     

Documentation generated on Fri, 11 Jul 2008 13:28:43 -0400 by phpDocumentor 1.4.2