AJAX:X 秒后出错时重新加载 load()
我使用以下函数通过 load() 从文件中获取信息。如果内容未按预期加载,是否会显示错误消息。这工作得很好,但我想在 X 秒后重新加载请求。我已经做到了这一点:
var tick = function() {
$('#fetch-1').hide();
$('#fetch-2').hide();
$('#fetch-3').hide();
$('#fetch-4').hide();
$('#fetch-5').hide();
$('#fetch-6').hide();
$('#fetch-1-error').show();
$('#fetch-2-error').show();
$('#fetch-3-error').show();
$('#fetch-4-error').show();
$('#fetch-5-error').show();
$('#fetch-6-error').show();
}
var loadTimeout = setTimeout(tick, 1000);
var tack = 1000;
$.ajax({
url: '/game/configs/required/jquery-fetch/fetch-1.php?i=<?php echo $row["id"]; ?>',
timeout: tack,
cache: false,
success: function(data) {
$('#fetch-tpb-filesize').load('/game/configs/required/jquery-fetch/fetch-1.php?i=<?php echo $row["id"]; ?>');
clearTimeout(loadTimeout);
},
error: function(data) {
setInterval(function() {
$('#fetch-1').show();
$('#fetch-1').load('/game/configs/required/jquery-fetch/fetch-1.php?i=<?php echo $row["id"]; ?>');
$('#fetch-1-error').hide();
}, 1000);
}
});
正如你在错误中看到的那样,我尝试让它重新加载,但它并没有像第一次那样在 1 秒后停止。这里有人知道如何解决这个问题吗?
I use the following function to get information from a file with load(). If the content doesn't load as it should, will it show a error message. This works perfectly but I want to reload the request after X seconds. I have done this far:
var tick = function() {
$('#fetch-1').hide();
$('#fetch-2').hide();
$('#fetch-3').hide();
$('#fetch-4').hide();
$('#fetch-5').hide();
$('#fetch-6').hide();
$('#fetch-1-error').show();
$('#fetch-2-error').show();
$('#fetch-3-error').show();
$('#fetch-4-error').show();
$('#fetch-5-error').show();
$('#fetch-6-error').show();
}
var loadTimeout = setTimeout(tick, 1000);
var tack = 1000;
$.ajax({
url: '/game/configs/required/jquery-fetch/fetch-1.php?i=<?php echo $row["id"]; ?>',
timeout: tack,
cache: false,
success: function(data) {
$('#fetch-tpb-filesize').load('/game/configs/required/jquery-fetch/fetch-1.php?i=<?php echo $row["id"]; ?>');
clearTimeout(loadTimeout);
},
error: function(data) {
setInterval(function() {
$('#fetch-1').show();
$('#fetch-1').load('/game/configs/required/jquery-fetch/fetch-1.php?i=<?php echo $row["id"]; ?>');
$('#fetch-1-error').hide();
}, 1000);
}
});
As you can see in error I have tried to make it reload, but it doesn't stop after 1 second as the first time. Any one here who knows how to fix the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在错误函数中使用
setInterval
,该函数每秒都会调用该代码块。您还可以使用 jQuery 方法来为您完成所有脏活。如果要使用timeout:tack,则需要再次使用$.ajax。为什么不把它变成一个函数并再次调用它,而不是到处复制和粘贴代码。
You are using
setInterval
inside the error function which will call that code block every second.Also you are using a jQuery method that does all of the dirty work for you. If you want to use timeout: tack, you need to use $.ajax again. Why don't you just make it a function and call it again instead of copy and pasting code and code and code all over the place.