1 /**
  2  * The Render Engine
  3  * MultiTimeout
  4  *
  5  * @fileoverview A multiple interval timeout timer which self-destroys.
  6  *
  7  * @author: Brett Fattori (brettf@renderengine.com)
  8  * @author: $Author: bfattori $
  9  * @version: $Revision: 1555 $
 10  *
 11  * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com)
 12  *
 13  * Permission is hereby granted, free of charge, to any person obtaining a copy
 14  * of this software and associated documentation files (the "Software"), to deal
 15  * in the Software without restriction, including without limitation the rights
 16  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 17  * copies of the Software, and to permit persons to whom the Software is
 18  * furnished to do so, subject to the following conditions:
 19  *
 20  * The above copyright notice and this permission notice shall be included in
 21  * all copies or substantial portions of the Software.
 22  *
 23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 26  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 28  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 29  * THE SOFTWARE.
 30  *
 31  */
 32 
 33 // The class this file defines and its required classes
 34 R.Engine.define({
 35     "class":"R.lang.MultiTimeout",
 36     "requires":[
 37         "R.lang.Timeout"
 38     ]
 39 });
 40 
 41 /**
 42  * @class An extension of {@link R.lang.Timeout} that will repeat the specified number of times before
 43  *        destroying itself.  The callback will be triggered with the
 44  *        repetition number as the only argument.  Within the callback, <tt>this</tt>
 45  *        refers to the <tt>Timer</tt> object itself.
 46  *
 47  * @param name {String} The name of the timer
 48  * @param reps {Number} The number of repetitions to restart the timer automatically
 49  * @param interval {Number} The interval for the timer, in milliseconds
 50  * @param callback {Function} The function to call when the interval is reached
 51  * @extends R.lang.Timeout
 52  * @constructor
 53  * @description Creat a multi-timeout triggering timer
 54  */
 55 R.lang.MultiTimeout = function () {
 56     "use strict";
 57     return R.lang.Timeout.extend(/** @scope R.lang.MultiTimeout.prototype */{
 58 
 59         /** @private */
 60         constructor:function (name, reps, interval, callback) {
 61 
 62             var timerObj = {
 63                 callback:callback,
 64                 repetitions:reps,
 65                 totalReps:0,
 66                 timer:this
 67             };
 68 
 69             var cb = R.bind(timerObj, function () {
 70                 if (this.repetitions-- > 0) {
 71                     this.callback.call(this.timer, this.totalReps);
 72                     this.totalReps++;
 73                     this.timer.restart();
 74                 }
 75                 else {
 76                     this.timer.destroy();
 77                 }
 78             });
 79 
 80             this.base(name, interval, cb);
 81         }
 82 
 83     }, /** @scope R.lang.MultiTimeout.prototype */ {
 84 
 85         /**
 86          * Get the class name of this object
 87          * @return {String} "R.lang.MultiTimeout"
 88          */
 89         getClassName:function () {
 90             return "R.lang.MultiTimeout";
 91         }
 92     });
 93 
 94 }