(たまたま 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 呼び出しを立て続けに開始したくないので、それはうまくいきません。前の呼び出しが失敗した場合にのみ、再試行が発生するようにします。