Javascript 在重试Ajax场景中,如何消除这种难看的重复代码?
由于Javascript 在重试Ajax场景中,如何消除这种难看的重复代码?,javascript,jquery,ajax,callback,jquery-callback,Javascript,Jquery,Ajax,Callback,Jquery Callback,由于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 d
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);
}
}
});
//加载玩家的游戏。
gc.api.getGamesByPlayerId(gc.game.player.id,gc.game.player.access_令牌,函数(数据){
如果(data.status_code===401){
//呼叫可能因呼叫速度过快而失败。请重试。。。
gc.api.getGamesByPlayerId(gc.game.player.id,gc.game.player.access_令牌,函数(数据){
如果(data.status_code===401){
//呼叫可能因呼叫速度过快而失败。请重试。。。
gc.api.getGamesByPlayerId(gc.game.player.id,gc.game.player.access_令牌,函数(数据){
如果(data.status_code===401){
//呼叫可能因呼叫速度过快而失败。请重试。。。
gc.api.getGamesByPlayerId(gc.game.player.id,gc.game.player.access_令牌,函数(数据){
如果(data.status_code===401){
//好的,可以安全地假设服务器是最新的,并且
//我们确实无权这样做。
警惕(“您未被授权”);
}否则{
//将游戏添加到HTML。
对于(变量i=0;i
通常情况下,我会考虑使用for循环,但这不会起作用,因为我不想快速连续地发出Ajax调用。我希望只有在前面的调用失败时才会触发重试。忽略需要连续多次发出相同请求的情况,您可能可以使用递归函数来实现这一点。例如,类似于:
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);
}
});
}
loadPlayerGames(4);
函数loadPlayerGames(特里斯勒夫特){
gc.api.getGamesByPlayerId(gc.game.player.id,gc.game.player.access_令牌,函数(数据){
如果(data.status_code!==401){
//将游戏添加到HTML。
对于(变量i=0;i }否则,如果(triesLeft忽略需要连续多次发出相同请求的情况,则可以使用递归函数来完成此操作。例如:
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);
}
});
}
loadPlayerGames(4);
函数loadPlayerGames(特里斯勒夫特){
gc.api.getGamesByPlayerId(gc.game.player.id,gc.game.player.access_令牌,函数(数据){
如果(data.status_code!==401){
//将游戏添加到HTML。
对于(变量i=0;i }否则如果(特里斯里夫特)我为什么没想到呢?谢谢!我为什么没想到呢?谢谢!