X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=0.6.x%2Fjs%2F01_core%2F14_XTimer.js;h=48b27a3124d63ac2be4def15e1bb6ff2b6471be3;hb=d836e6243878426d4cfd7a14ceb9b77db9f92b57;hp=9dbfe4042045f5e0bb57aaa9661d7ae98bb0bc2f;hpb=dc5a75639232882249108b4f708916e9690e42b3;p=pettanr%2FclientJs.git diff --git a/0.6.x/js/01_core/14_XTimer.js b/0.6.x/js/01_core/14_XTimer.js index 9dbfe40..48b27a3 100644 --- a/0.6.x/js/01_core/14_XTimer.js +++ b/0.6.x/js/01_core/14_XTimer.js @@ -1,4 +1,4 @@ -/** +/* * use X.Callback */ @@ -34,7 +34,15 @@ // ------------ local variables -------------------------------------------- // // ------------------------------------------------------------------------- // -var X_Timer_now = Date.now || function(){ return +new Date; }, +var + + /** + * 現在時の ms を返します。 new Date().getTime() の値です。 + * @alias X.Timer.now + * @function + * @return {number} ミリ秒 + */ + X_Timer_now = Date.now || ( function(){ return +new Date; } ), X_Timer_SET_TIMEOUT = window.setTimeout, X_Timer_CLEAR_TIMEOUT = window.clearTimeout, @@ -71,89 +79,54 @@ var X_Timer_now = Date.now || function(){ return +new Date; }, X_Timer_REQ_FRAME_LIST = [], X_Timer_requestID = 0, - X_Timer_busyOnFrame = false; + X_Timer_busyOnFrame = false, -// ------------------------------------------------------------------------- // -// --- interface ----------------------------------------------------------- // -// ------------------------------------------------------------------------- // -X.Timer = { - - now : X_Timer_now, - - add : function( time, opt_count, args1, args2, args3 ){ - var list = X_Timer_TICKET_LIST, - hash, obj; - time = time < X_Timer_INTERVAL_TIME ? 1 : time / X_Timer_INTERVAL_TIME | 0; // 正の数で使える「Math.floor(x)」を「(x | 0)」に; - - if( !X.Type.isNumber( opt_count ) ){ - args3 = args2; - args2 = args1; - args1 = opt_count; - opt_count = 0; - }; - - hash = X_Callback_classifyCallbackArgs( args1, args2, args3 ); - if( !hash ) return -1; // dev only - - if( !hash.k ) hash = { f : hash }; - hash.time = time; - hash.last = time; - hash.count = opt_count; - hash.uid = ++X_Timer_uid; - list[ list.length ] = hash; - - !X_Timer_busyTimeout && X_Timer_update(); - return X_Timer_uid; - }, - - once : function( time, args1, args2, args3 ){ - return X.Timer.add( time, 1, args1, args2, args3 ); - }, - - remove : function( uid ){ - var list = X_Timer_TICKET_LIST, - i = list.length, - l = i, - f, q; - // fire 中の cancel - if( X_Timer_busyTimeout ){ - if( !X_Timer_removal ) X_Timer_removal = {}; - X_Timer_removal[ uid ] = true; - } else { - for( ; i; ){ - if( ( q = list[ --i ] ).uid === uid ){ - list.splice( i, 1 ); - !X_Timer_skipUpdate && ( q.last <= X_Timer_waitTime || l === 1 ) && X_Timer_update(); - break; - }; - }; - }; - }, - - requestFrame : X_Timer_REQ_ANIME_FRAME ? + /** + * requestAnimationFrame をセットします。 + * @alias X.Timer.requestFrame + * @function + * @param {*} args1 コールバックのための最大で 3 つの引数を指定します。参考:__CallbackHash__ + * @param {*} args2 + * @param {*} args3 + * @return {number} タイマーID。1 以上の数値。タイマーの解除に使用。 + */ + X_Timer_requestFrame = X_Timer_REQ_ANIME_FRAME ? (function( args1, args2, args3 ){ var i = X_Timer_REQ_FRAME_LIST.length, f; i === 0 && ( X_Timer_requestID = X_Timer_REQ_ANIME_FRAME( X_Timer_onEnterFrame ) ); - f = X_Timer_REQ_FRAME_LIST[ i ] = X_Callback_classifyCallbackArgs( args1, args2, args3 ); + f = X_Callback_classifyCallbackArgs( args1, args2, args3 ); + if( !f.kind ) f = { func : f }; + X_Timer_REQ_FRAME_LIST[ i ] = f; return f.uid = ++X_Timer_uid; }) : (function( args1, args2, args3 ){ var i = X_Timer_REQ_FRAME_LIST.length, f; - i === 0 && ( X_Timer_requestID = X.Timer.add( 0, 1, X_Timer_onEnterFrame ) ); - f = X_Timer_REQ_FRAME_LIST[ i ] = X_Callback_classifyCallbackArgs( args1, args2, args3 ); + i === 0 && ( X_Timer_requestID = X_Timer_add( 0, 1, X_Timer_onEnterFrame ) ); + f = X_Callback_classifyCallbackArgs( args1, args2, args3 ); + if( !f.kind ) f = { func : f }; + X_Timer_REQ_FRAME_LIST[ i ] = f; return f.uid = ++X_Timer_uid; }), - - cancelFrame : X_Timer_CANCEL_ANIME_FRAME ? + + /** + * requestAnimationFrame を解除します。登録時に受け取ったタイマーIDを使用します。 + * @alias X.Timer.cancelFrame + * @function + * @param {number|string} タイマーID, 数字文字の場合もある! + * @return {number} 0 が返る + * @example if( timerID ) timerID = X.Timer.cancelFrame( timerID ); + */ + X_Timer_cancelFrame = X_Timer_CANCEL_ANIME_FRAME ? (function( uid ){ var list = X_Timer_REQ_FRAME_LIST, l = list.length, i = l, f; - // fire 中の cancel + if( X_Timer_busyOnFrame ){ + // fire 中の cancel if( !X_Timer_removal ) X_Timer_removal = {}; X_Timer_removal[ uid ] = true; } else { @@ -161,20 +134,22 @@ X.Timer = { if( ( f = list[ --i ] ).uid < uid ) break; if( f.uid === uid ){ list.splice( i, 1 ); - // gecko では無い場合がある + // gecko では cancelRequestAnimationFrame が無い場合がある l === 1 && X_Timer_CANCEL_ANIME_FRAME && X_Timer_CANCEL_ANIME_FRAME( X_Timer_requestID ); break; }; }; }; + return 0; }) : (function( uid ){ var list = X_Timer_REQ_FRAME_LIST, l = list.length, i = l, f; - // fire 中の cancel + if( X_Timer_busyOnFrame ){ + // fire 中の cancel if( !X_Timer_removal ) X_Timer_removal = {}; X_Timer_removal[ uid ] = true; } else { @@ -182,12 +157,43 @@ X.Timer = { if( ( f = list[ --i ] ).uid < uid ) break; if( f.uid === uid ){ list.splice( i, 1 ); - l === 1 && X.Timer.remove( X_Timer_requestID ); + l === 1 && X_Timer_remove( X_Timer_requestID ); break; }; }; }; - }) + return 0; + }); + + +// ------------------------------------------------------------------------- // +// --- interface ----------------------------------------------------------- // +// ------------------------------------------------------------------------- // + +/** + *

setTimeout をラップします。複数のタイマーを登録しても Web ブラウザにはひとつのタイマーを登録します。 + *

参考:複雑で重くなった JavaScript を超高速化する方法3, + * [JavaScript]setIntervalを実験する + *

指定時間の経過したタイマーは、より過去のものから順番にコールバックされます。 + *

setTimeout のコールバックに文字列しか指定できないブラウザがあり対策しています。 + *

requestAnimationFrame をラップします。ベンダープレフィックス付の requestAnimationFrame もない場合、setTimeout にフォールバックします。 + * + * @namespace X.Timer + * @alias X.Timer + */ +X[ 'Timer' ] = { + + 'now' : X_Timer_now, + + 'add' : X_Timer_add, + + 'once' : X_Timer_once, + + 'remove' : X_Timer_remove, + + 'requestFrame' : X_Timer_requestFrame, + + 'cancelFrame' : X_Timer_cancelFrame }; @@ -195,7 +201,94 @@ X.Timer = { // --- implements ---------------------------------------------------------- // // ------------------------------------------------------------------------- // -if( X_UA.IE4 || X_UA.MacIE ){ + /** + * タイマーをセットします。 + * @alias X.Timer.add + * @param {number} time ミリ秒 + * @param {number} opt_count 回数。省略可能。指定回数で自動でタイマーを破棄します。0 を指定した場合無限にタイマーが呼ばれます。省略した場合 0 と同じです。 + * @param {*} args1 コールバックのための最大で 3 つの引数を指定します。参考:__CallbackHash__ + * @param {*} args2 + * @param {*} args3 + * @return {number} タイマーID。1 以上の数値。タイマーの解除に使用。 + * @example timerID = X.Timer.add( 1000, 5, thisContext, onTimer ); + */ + function X_Timer_add( time, opt_count, args1, args2, args3 ){ + var list = X_Timer_TICKET_LIST, + hash, obj; + time = time < X_Timer_INTERVAL_TIME ? 1 : time / X_Timer_INTERVAL_TIME | 0; // 正の数で使える「Math.floor(x)」を「(x | 0)」に; + + if( !X_Type_isNumber( opt_count ) ){ + args3 = args2; + args2 = args1; + args1 = opt_count; + opt_count = 0; + }; + + hash = X_Callback_classifyCallbackArgs( args1, args2, args3 ); + if( !hash ) return -1; // dev only + + if( !hash.kind ) hash = { func : hash }; + hash.time = time; + hash.last = time; + hash.count = opt_count; + hash.uid = ++X_Timer_uid; + list[ list.length ] = hash; + + !X_Timer_busyTimeout && X_Timer_update(); + return X_Timer_uid; + }; + + /** + * 1 回呼ばれたら解除されるタイマーをセットします。 + * @alias X.Timer.once + * @param {number} time ミリ秒 + * @param {*} args1 コールバックのための最大で 3 つの引数を指定します。参考:__CallbackHash__ + * @param {*} args2 + * @param {*} args3 + * @return {number} タイマーID。1 以上の数値。タイマーの解除に使用。 + */ + function X_Timer_once( time, args1, args2, args3 ){ + return X_Timer_add( time, 1, args1, args2, args3 ); + }; + + /** + * タイマーを解除します。登録時に受け取ったタイマーIDを使用します。 + * @alias X.Timer.remove + * @param {number} タイマーID + * @return {number} 0 が返る + * @example if( timerID ) timerID = X.Timer.remove( timerID ); + */ + function X_Timer_remove( uid ){ + var list = X_Timer_TICKET_LIST, + i = list.length, + l = i, + f, q, eventDispatcher, lazy, listeners; + + if( X_Timer_busyTimeout ){ + // fire 中の cancel + if( !X_Timer_removal ) X_Timer_removal = {}; + X_Timer_removal[ uid ] = true; + } else { + for( ; i; ){ + if( ( q = list[ --i ] ).uid === uid ){ + list.splice( i, 1 ); + + /* + * lazyDispatch 中の EventDispatcher の有無を調べる + */ + if( X_EventDispatcher_LAZY_TIMERS[ uid ] ){ + delete X_EventDispatcher_LAZY_TIMERS[ uid ]; + }; + + !X_Timer_skipUpdate && ( q.last <= X_Timer_waitTime || l === 1 ) && X_Timer_update(); + break; + }; + }; + }; + return 0; + }; + +if( X_UA[ 'IE4' ] || X_UA[ 'MacIE' ] ){ X.Timer[ '_' ] = X_Timer_onTimeout; X_Timer_onTimeout = 'X.Timer._()'; }; @@ -231,10 +324,10 @@ function X_Timer_onTimeout(){ X_Timer_currentUID = q.uid; - if( q.k ){ + if( q.kind ){ r = X_Callback_proxyCallback( q, [] ); } else { - r = q.f(); + r = q.func(); }; //console.log( 'fire....' ); @@ -261,7 +354,7 @@ function X_Timer_onTimeout(){ X_Timer_skipUpdate = true; for( uid in X_Timer_removal ){ //if( X_EMPTY_OBJECT[ uid ] ) continue; - X.Timer.remove( uid ); + X_Timer_remove( uid ); }; X_Timer_skipUpdate = false; X_Timer_removal = null; @@ -302,7 +395,7 @@ function X_Timer_compareQueue( a, b ){ // http://havelog.ayumusato.com/develop/javascript/e528-ios6_scrolling_timer_notcall.html // iOS6 スクロール中のタイマー発火絡みのバグ備忘 -if( X_UA.iOS ){ +if( X_UA[ 'iOS' ] ){ window.addEventListener( 'scroll', function(){ var last, now; if( X_Timer_timerId ){ @@ -332,23 +425,23 @@ function X_Timer_onEnterFrame( time ){ if( X_Timer_removal && X_Timer_removal[ q.uid ] ) continue; - if( q.k ){ + if( q.kind ){ X_Callback_proxyCallback( q, args || ( args = [ time ] ) ); } else { - q( time ); + q.func( time ); }; }; list.splice( 0, l ); if( list.length ){ - X_Timer_requestID = X_Timer_REQ_ANIME_FRAME ? X_Timer_REQ_ANIME_FRAME( X_Timer_onEnterFrame ) : X.Timer.add( 0, 1, X_Timer_onEnterFrame ); + X_Timer_requestID = X_Timer_REQ_ANIME_FRAME ? X_Timer_REQ_ANIME_FRAME( X_Timer_onEnterFrame ) : X_Timer_add( 0, 1, X_Timer_onEnterFrame ); }; X_Timer_busyOnFrame = false; if( X_Timer_removal ){ for( uid in X_Timer_removal ){ //if( X_EMPTY_OBJECT[ uid ] ) continue; - X.Timer.cancelFrame( X_Timer_removal[ uid ] ); + X_Timer_cancelFrame( X_Timer_removal[ uid ] ); }; X_Timer_removal = null; };