如何在重试 Ajax 场景中消除这种丑陋的重复代码
How can I eliminate this ugly duplicate code during a retry-Ajax scenario?
由于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 调用。我希望仅在上述调用失败时触发重试。
忽略需要连续多次发出相同请求的情况,您可能可以使用递归函数来实现这一点。例如,类似这样的内容:
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);
}
});
}
相关文章:
- JavaScript:在源404上重试
- 用分隔符分隔具有多个整数值的字符串的Javascript"重试错误的值
- Ajax在NodeJS中为一个耗时的请求请求多次重试
- 将以下代码重置并刷新提交后的表单
- 如何仅在RxJs中可观察到的源发出的特定错误上重试
- BreezeJS中央错误处理程序和自动重试
- 如何捕获请求中的错误,然后打开模态,然后在模态使用RxJS关闭时重试
- Angular2 http重试逻辑
- JS重试函数几次,看看它是否返回true
- Promise中的递归重试
- Bluebird Promise 重试 DocumentDB 请求
- 如果发生错误,如何重试 DriveApp.getFileById,直到成功
- Nodejs - 让客户端套接字在 5 秒超时后重试
- 重试时更改 ajax 数据
- RxJS 重试运算符与 ajax 调用
- 如何添加将通过执行 JavaScript 代码重定向到另一个页面的动态操作
- JS/JQuery 重试 1 秒后 img 加载
- JavaScript重试承诺,直到解决
- 节点 JS:使用状态代码 503 重试函数
- 如何在重试 Ajax 场景中消除这种丑陋的重复代码