0

(たまたま Ajax 呼び出しである)のコールバック呼び出しの性質のgetGamesByPlayerIdため、次のコードの重複を排除する方法がわかりません。

// Load the player's games.
gc.api.getGamesByPlayerId(gc.game.player.id, gc.game.player.access_token, function(data) {

    if(data.status_code === 401) {

        // Call may have failed due to being called too fast. Retry...
        gc.api.getGamesByPlayerId(gc.game.player.id, gc.game.player.access_token, function(data) {

            if(data.status_code === 401) {

                // Call may have failed due to being called too fast. Retry...
                gc.api.getGamesByPlayerId(gc.game.player.id, gc.game.player.access_token, function(data) {

                    if(data.status_code === 401) {

                        // Call may have failed due to being called too fast. Retry...
                        gc.api.getGamesByPlayerId(gc.game.player.id, gc.game.player.access_token, function(data) {

                            if(data.status_code === 401) {

                                // OK. It's safe to assume the server is current, and that
                                // we truly are not authorized to do this.
                                alert("You are not authorized.");

                            } else {

                                // Add games to HTML.
                                for( var i = 0; i < data.length; i++ ) {

                                    var html = '<li><a href="?g=' + data[i].id + '">' + data[i].id + '</a></li>';

                                    $('#games').append(html);

                                }

                            }

                        });

                    } else {

                        // Add games to HTML.
                        for( var i = 0; i < data.length; i++ ) {

                            var html = '<li><a href="?g=' + data[i].id + '">' + data[i].id + '</a></li>';

                            $('#games').append(html);

                        }

                    }

                });

            } else {

                // Add games to HTML.
                for( var i = 0; i < data.length; i++ ) {

                    var html = '<li><a href="?g=' + data[i].id + '">' + data[i].id + '</a></li>';

                    $('#games').append(html);

                }

            }

        });

    } else {

        // Add games to HTML.
        for( var i = 0; i < data.length; i++ ) {

            var html = '<li><a href="?g=' + data[i].id + '">' + data[i].id + '</a></li>';

            $('#games').append(html);

        }

    }

});

通常、私は for ループを使用することを考えますが、Ajax 呼び出しを立て続けに開始したくないので、それはうまくいきません。前の呼び出しが失敗した場合にのみ、再試行が発生するようにします。

4

1 に答える 1

3

同じリクエストを連続して複数回行う必要がある状況を無視すると、おそらく再帰関数を使用してこれを達成できます。たとえば、次のようなものです。

loadPlayerGames(4);

function loadPlayerGames(triesLeft) {
    gc.api.getGamesByPlayerId(gc.game.player.id, gc.game.player.access_token, function(data) {
        if(data.status_code !== 401) {
            // Add games to HTML.
            for( var i = 0; i < data.length; i++ ) {
                var html = '<li><a href="?g=' + data[i].id + '">' + data[i].id + '</a></li>';
                $('#games').append(html);
            }
        } else if(triesLeft <= 0) {
            // OK. It's safe to assume the server is current, and that
            // we truly are not authorized to do this.
            alert("You are not authorized.");
        } else {
            // Call may have failed due to being called too fast. Retry...
            loadPlayerGames(triesLeft - 1);
        }
    });
}
于 2013-02-01T00:27:19.583 に答える