| « 非同期ループ処理 (4) - 進捗表示1 | 非同期ループ処理 (6) - 列挙型 » |
非同期ループ処理 (5) - 進捗表示2
非同期ループ処理 (4) と似ているが、配列から shift して要素を取り出すのではなく、配列全体を保持しつつ位置 _index を加算しながら要素を取り出している。この方法でもプログレスバーなどで進捗状況を表示できる。
var asyncProgressiveProcessor2 = {
_index : -1,
_array : [],
start : function(aArray)
{
// 開始処理
dump("start
");
// 初期化
this._array = aArray;
this._index = -1;
this._next();
},
_next : function()
{
if ( ++this._index < this._array.length ) {
setTimeout(function(){ asyncProgressiveProcessor2._process(); }, 500);
} else {
setTimeout(function(){ asyncProgressiveProcessor2._finish(); }, 500);
}
},
_process : function()
{
var elt = this._array[this._index];
// 処理
dump("processing (" + (this._index+1) + "/" + this._array.length + ")... " + elt + "
");
// 次の処理へ
this._next();
},
_finish : function()
{
// 終了処理
dump("finish
");
},
};
asyncProgressiveProcessor2.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']);