Pot.repeat

{Void|*} Pot.repeat ({Number|Object} max, {Function} callback [, {Object|*} context])
{Pot.Deferred} Pot.Deferred.repeat ({Number|Object} max, {Function} callback [, {Object|*} context])
{Pot.Deferred} Pot.Deferred.prototype.repeat ({Function} callback [, {Object|*} context])

Pot.js と PotLite.js で利用可能。

引数 max を最大数とし、max 回、ループ処理を実行します。

引数 max を最大数として、最大 max 回、コールバック関数 callback を実行します。
処理は、for 文、もしくは for-of のように実行されます。
引数 context が指定されると、callback 関数の this が context にとって代わります。
例えば、10 回繰り返し処理を行うとすると

for (var i = 0; i < 10; i++) { debug(i); }
Pot.repeat(10, function(i) { debug(i) });

この 2 つの例の出力結果は同じになります。

引数 max には、数値以外にオブジェクトを渡すことができます。

var result = [];

Pot.repeat({begin: 0, end: 100, step: 10}, function(i) {
    result.push(i);
});
debug(result); // [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
引数 max をオブジェクトで指定する場合:

begin : {Number}  開始する数値。省略時は 0 になります。
step  : {Number}  1 ループごとのステップ。省略時は 1 になります。
end   : {Number}  終了する数値。省略時は 0 になります。

これらを指定することで、例えば 重い for 文を Pot.Deferred.repeat に置き換える際に役立ちます。

コールバック関数の引数は以下のようになります。

function (i, last, loops) { this === context }

    i     : {Number}   step or 0 から始まって 1 ずつ繰り上がる/繰り下がる数値
    last  : {Boolean}  ループの最後なら true になる値
    loops : {Object}   ループに関する情報

処理は 同期処理、非同期処理、それぞれ異なります。

  • Pot.repeat は同期処理になります。
    Pot.globalize() 適応済みの場合は repeat() で実行できます。
  • Pot.Deferred.repeat は非同期処理になり、Pot.Deferred のインスタンスを返します。
    Pot.globalize() 適応済みの場合は Deferred.repeat() で実行できます。
  • Pot.Deferred.prototype.repeat は、Pot.Deferred.prototype.async() によって 同期/非同期 が変化します。
    Pot.Deferred チェイン上で実行でき、Pot.Deferred のインスタンスを返します。

callback 関数の中で StopIteration (Pot.StopIteration) を throw することでループを止められます。

Pot.Deferred.repeat.slow() のようにして速度の指定ができます。
非同期では、ほとんどの環境で速度指定が可能ですが、 同期での速度指定は一部の環境 (XUL 上など) のみになります。
指定できる名前は以下の表を参照ください。
デフォルトの速度は normal です。

値 / メソッド名 速度
limp 最も遅い
doze 遅い
slow 遅め
normal 通常
fast 速め
rapid 速い
ninja 最も速い

戻り値はありません。

Pot.repeat で同期処理の例:

var result = [];

Pot.repeat(10, function(i) {
    result.push(i);
});
debug(result); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
// コールバック第二引数 last を利用した例

var string = '';
var chars = 'abcdef'.split('');

Pot.repeat(chars.length, function(i, last) {
    string += chars[i] + '=' + i + (last ? ';' : ',');
});
debug(string); // 'a=0,b=1,c=2,d=3,e=4,f=5;'
// オブジェクトで指定した例

var result = [];

Pot.repeat({begin: 0, end: 100, step: 10}, function(i) {
    result.push(i);
});
debug(result); // [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

Pot.Deferred.repeat で非同期処理の例:

var result = [];

Pot.Deferred.repeat(10, function(i) {
    result.push(i);
}).then(function() {
    debug(result); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
});
// コールバック第二引数 last を利用した例

var string = '';
var chars = 'abcdef'.split('');

Pot.Deferred.repeat(chars.length, function(i, last) {
    string += chars[i] + '=' + i + (last ? ';' : ',');
}).then(function() {
    debug(string); // 'a=0,b=1,c=2,d=3,e=4,f=5;'
});
// オブジェクトで指定した例

var result = [];

Pot.Deferred.repeat({begin: 0, end: 100, step: 10}, function(i) {
    result.push(i);
}).then(function() {
    debug(result); // [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
});

Pot.Deferred.prototype.repeat で非同期処理の例:

var result = [];

var d = new Pot.Deferred();

d.then(function() {
    return 10;
}).repeat(function(i) {
    result.push(i);
}).then(function() {
    debug(result); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}).begin();
// コールバック第二引数 last を利用した例

var string = '';
var chars = 'abcdef'.split('');

var d = new Pot.Deferred();

d.repeat(function(i, last) {
    string += chars[i] + '=' + i + (last ? ';' : ',');
}).then(function() {
    debug(string); // 'a=0,b=1,c=2,d=3,e=4,f=5;'
}).begin(chars);
// オブジェクトで指定した例

var result = [];

Pot.Deferred.begin(function() {
    return {
        begin : 0,
        end   : 100,
        step  : 10
    };
}).repeat(function(i) {
    result.push(i);
}).then(function() {
    debug(result); // [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
});