タイマーを考えてたら、メモとして。

var Timer = function ( callbackfn, wait, span, flag ) {

  this.init = function ( _callbackfn, _wait, _span, _flag ) {
    this.stop();

    if( 'function' === typeof _callbackfn ) 
      callbackfn = _callbackfn;

    if( 'number' === typeof _wait )
      wait = _wait;

    if( 'number' === typeof _span )
      span = _span;

    if( 'undefined' !== typeof flag ) flag = !! flag;
    
    if( 0 == wait ) this.start();
    if( flag ) this.wait();
    return null;
  };


  this.wait = function ( ) {
    this.stop( );
    return tmid_w = setTimeout( (function ( that ) {
      return function ( ) {
        that.start( );
      };
    })( this ), wait );
  };
  
  
  this.start = function ( ) {
    return ( 'function' === typeof callbackfn ) ?
      tmid_s = setInterval( (function (_callbackfn ) {
        return function ( ) {
          _callbackfn();
        };
      })( callbackfn  ), span ): false;
  };
  
  
  this.stop = function ( ) {
    tmid_w && clearTimeout( tmid_w );
    tmid_s && clearInterval( tmid_s );
    return null;
  };

  var tmid_w = null;
  var tmid_s = null;
  
  this.init.apply( this, arguments );
  if( flag ) this.wait();
};