| « 非同期ループ処理 (6) - 列挙型 | DOMノードのアイソレート » |
非同期ループ処理 (7) - 同期非同期複合型
これまで述べてきた非同期ループ処理の問題点として、 setTimeout の間隔を0ミリ秒にしたところで、全体としては単純な for ループよりもかなり時間がかかってしまうことである。
そこで、 setTimeout しない同期的なループ処理も組み合わせて適度に高速化を図る。
下記の例では3回の処理を一単位としている。
var syncAsyncProcessor = {
_array : [],
_count : -1,
start : function(aArray)
{
// 開始処理
dump("start
");
// 初期化
this._count = -1;
this._array = aArray;
this._next();
},
_next : function()
{
var elt = this._array.shift();
if ( elt ) {
if ( ++this._count % 3 == 0 )
// 数回に一度、非同期
setTimeout(function(){ syncAsyncProcessor._process(elt); }, 1000);
else
// それ以外は同期
syncAsyncProcessor._process(elt);
} else {
setTimeout(function(){ syncAsyncProcessor._finish(); }, 1000);
}
},
_process : function(aElt)
{
// 処理
dump("processing (" + this._count + ")... " + aElt + "
");
// 次の処理へ
this._next();
},
_finish : function()
{
// 終了処理
dump("finish
");
},
};
syncAsyncProcessor.start(['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']);
I tried to apply this concept on my code, but doesn’t function. You could help me?
My code:
—–
—–
Many thanks!
Cheers,
v1d4l0k4