由于getGamesByPlayerId
(恰好是 Ajax 调用)的回调调用性质,我似乎无法弄清楚如何消除以下重复代码:
// 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 调用。我希望仅在前面的调用失败时重试触发。