使用setInterval()在jQuery AJAX中重定向(轮询)
我有一个使用setInterval的简单队列系统,使用jQueryAjax检查后端的队列。当满足某些条件时,我想重定向到一个页面,在该页面上该用户可以继续该过程 在Firefox上,我得到了预期的重定向,但在IE和Chrome上没有。IE简单地给出了一个“此网页无法显示”错误,而Chrome给出了一个“此网页有重定向循环”错误 你知道怎么回事吗 这是我的密码:使用setInterval()在jQuery AJAX中重定向(轮询),jquery,ajax,asp.net-mvc,redirect,Jquery,Ajax,Asp.net Mvc,Redirect,我有一个使用setInterval的简单队列系统,使用jQueryAjax检查后端的队列。当满足某些条件时,我想重定向到一个页面,在该页面上该用户可以继续该过程 在Firefox上,我得到了预期的重定向,但在IE和Chrome上没有。IE简单地给出了一个“此网页无法显示”错误,而Chrome给出了一个“此网页有重定向循环”错误 你知道怎么回事吗 这是我的密码: $(document).ready(function () { StartTheTimer(); function S
$(document).ready(function () {
StartTheTimer();
function StartTheTimer(e) {
var sec = parseInt($("#RefreshSeconds").val()) + 1;
var timer = setInterval(function () {
$('#spanQueueCountdown').text(--sec);
if (sec == 1) {
$('#spanSeconds').text('second');
}
else {
$('#spanSeconds').text('seconds');
}
if (sec == 0) {
clearInterval(timer);
$.ajax({
type: "POST",
url: "/Purchase/CheckQueue",
data: {
applicationSessionId: $("#ApplicationSessionId").val(),
eventId: $("#EventId").val(),
ordernumber: $("#OrderNumber").val()
},
cache: false,
dataType: "json",
success: function (result, textStatus, jqXHR) {
if (result.success && jqXHR.status == 200) {
if (!result.redirect) {
$("#spanQueueNumber").text(result.positionInQueue);
StartTheTimer();
}
else {
window.location.replace(result.redirecturl);
}
}
else {
console.log('Checking the queue failed.')
window.location.href = "/";
}
},
error: function () {
console.log('Error in checking queue.')
window.location.href = "/";
}
});
}
if ($('#spanQueueNumber').text() == 0) {
clearInterval(timer);
$('#spanQueueCountdown').text(0);
}
}, 1000);
}
});
您缺少结束括号}启动计时器方法后我找到了问题的解决方案,但并不理想 我在页面中添加了一个额外的div,其中包含一个链接,一旦用户位于队列前面,就需要遵循该链接。我能够从ajax成功方法中隐藏解释队列中的过程的部分,然后显示包含要遵循的链接的div 但是,请求的行为是,一旦用户到达队列的前端,页面就会自动重定向。所以这是一种妥协
如果有人能向我解释为什么我试图实现的目标是不可能的,请解释。否则,如果有其他人知道我做错了什么,请也这么做。我很乐意给客户他所期望的。用我目前拥有的更新了脚本,但仍然没有重定向