1 // ==========================================================================
  2 // Project:   SproutCore - JavaScript Application Framework
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            Portions ©2008-2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 
  8 SC.mixin(Function.prototype, /** @scope Function.prototype */ {
  9 
 10   /**
 11     Creates a timer that will execute the function after a specified 
 12     period of time.
 13     
 14     If you pass an optional set of arguments, the arguments will be passed
 15     to the function as well.  Otherwise the function should have the 
 16     signature:
 17     
 18         function functionName(timer)
 19 
 20     @param target {Object} optional target object to use as this
 21     @param interval {Number} the time to wait, in msec
 22     @returns {SC.Timer} scheduled timer
 23   */
 24   invokeLater: function(target, interval) {
 25     if (interval === undefined) interval = 1 ;
 26     var f = this;
 27     if (arguments.length > 2) {
 28       var args = SC.$A(arguments).slice(2,arguments.length);
 29       args.unshift(target);
 30       // f = f.bind.apply(f, args) ;
 31       var func = f ;
 32       // Use "this" in inner func because it get its scope by 
 33       // outer func f (=target). Could replace "this" with target for clarity.
 34       f = function() { return func.apply(this, args.slice(1)); } ;
 35     }
 36     return SC.Timer.schedule({ target: target, action: f, interval: interval });
 37   }
 38 
 39 });
 40