| « 非同期ループ処理 (3) | 非同期ループ処理 (5) - 進捗表示2 » |
非同期ループ処理 (4) - 進捗表示1
非同期ループ処理 (3) に、配列の長さ _length と現在の処理数 _count を追加した。
これによってプログレスバーなどで進捗状況を表示できるようになる。
var asyncProgressiveProcessor = {
_count : 0,
_length : 0,
_array : [],
start : function(aArray)
{
// 開始処理
dump("start
");
// 初期化
this._array = aArray;
this._count = 0;
this._length = this._array.length;
this._next();
},
_next : function()
{
var elt = this._array.shift();
if ( elt ) {
this._count++;
setTimeout(function(){ asyncProgressiveProcessor._process(elt); }, 500);
} else {
setTimeout(function(){ asyncProgressiveProcessor._finish(); }, 500);
}
},
_process : function(aElt)
{
// 処理
dump("processing (" + this._count + "/" + this._length + ")... " + aElt + "
");
// 次の処理へ
this._next();
},
_finish : function()
{
// 終了処理
dump("finish
");
},
};
asyncProgressiveProcessor.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']);