home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / jsmodules / SBTimer.jsm < prev    next >
Text File  |  2012-06-08  |  3KB  |  122 lines

  1. /*
  2. //
  3. // BEGIN SONGBIRD GPL
  4. //
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright(c) 2005-2008 POTI, Inc.
  8. // http://songbirdnest.com
  9. //
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the "GPL").
  12. //
  13. // Software distributed under the License is distributed
  14. // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
  15. // express or implied. See the GPL for the specific language
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc.,
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. //
  23. // END SONGBIRD GPL
  24. //
  25. */
  26.  
  27. EXPORTED_SYMBOLS = ["SBTimer"];
  28.  
  29. const Cc = Components.classes;
  30. const Ci = Components.interfaces;
  31. const Cr = Components.results
  32.  
  33. //------------------------------------------------------------------------------
  34. //
  35. // SBTimer class.
  36. //
  37. //   SBTimer objects provide a wrapper for XPCOM nsITimer objects to allow
  38. // functions with closures to be called when the timer expires, much like
  39. // window.setInterval.  However, SBTimer objects have the same precision as
  40. // nsITimer and can be used from XPCOM components.
  41. //
  42. //------------------------------------------------------------------------------
  43.  
  44. /**
  45.  * \brief Construct an SBTimer object with the timer type specified by aType
  46.  *        that will expire after the delay specified in milliseconds by aDelay.
  47.  *        Call the function specified by aFunc when the timer expires.
  48.  *        The timer type values are the same as for nsITimer.
  49.  *
  50.  * \param aFunc                 Function to call upon timer expiration.
  51.  * \param aDelay                Timer expiration delay in milliseconds.
  52.  * \param aType                 Type of timer (see nsITimer).
  53.  */
  54.  
  55. function SBTimer(aFunc, aDelay, aType) {
  56.   // Create the timer XPCOM object.
  57.   this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  58.  
  59.   // Set up the timer object fields.
  60.   this._timerFunc = aFunc;
  61.  
  62.   // Initialize the timer XPCOM object.
  63.   this._timer.init(this, aDelay, aType);
  64. }
  65.  
  66. SBTimer.prototype = {
  67.   //
  68.   // Timer object fields.
  69.   //
  70.   //   _timer                   Timer nsITimer XPCOM object.
  71.   //   _timerFunc               Function to call upon timer expiration.
  72.   //
  73.  
  74.   _timer: null,
  75.   _timerFunc: null,
  76.  
  77.  
  78.   //----------------------------------------------------------------------------
  79.   //
  80.   // Public timer functions.
  81.   //
  82.   //----------------------------------------------------------------------------
  83.  
  84.   /**
  85.    * \brief Cancel the timer.
  86.    */
  87.  
  88.   cancel: function SBTimer_cancel() {
  89.     this._timer.cancel();
  90.     this._timer = null;
  91.     this._timerFunc = null;
  92.   },
  93.  
  94.  
  95.   /**
  96.    * \brief Set the timer delay to the value specified by aDelay.
  97.    *
  98.    * \param aDelay              Delay in milliseconds.
  99.    */
  100.  
  101.   setDelay: function SBTimer_setDelay(aDelay) {
  102.     this._timer.delay = aDelay;
  103.   },
  104.  
  105.  
  106.   //----------------------------------------------------------------------------
  107.   //
  108.   // Timer nsIObserver functions.
  109.   //
  110.   //----------------------------------------------------------------------------
  111.  
  112.   /**
  113.    * \brief Observe timer events.  Ignore arguments.
  114.    */
  115.  
  116.   observe: function SBTimer_observe(aSubject, aTopic, aData) {
  117.     // Handle the timer event.
  118.     this._timerFunc.apply(null);
  119.   }
  120. };
  121.  
  122.