如何中断 Rails 中的 AJAX 请求?
我遇到以下情况:
选项卡式视图在单击选项卡以更改视图时发送 AJAX 请求,从用户单击选项卡到刷新视图,每个请求大约需要 3-4 秒(这是健康的,并且在我的系统中被接受)案例!)
我的问题是:
如果用户在加载 tab1 时单击 tab2,则用户希望看到 tab2 的内容并忽略第一次单击 tab1。
发生的情况是,tab1 的内容首先出现,然后是 tab2 的内容。我想阻止这种行为。
我的框架包含: Ruby 1.8.7 - Rails 2.3.5 - jQuery 1.5.2 (用于执行 AJAX 操作)
我尝试使用类似的代码,但它对我不起作用
function doRequest(){
if (lastPageAjaxRequest){
lastPageAjaxRequest.abort();
lastPageAjaxRequest = null;
}
lastPageAjaxRequest = $.ajax(..);
}
I've the following situation:
A tabbed view sends AJAX requests upon clicking on tabs to change view, each request takes around 3-4 seconds from the time the user clicks on the tab until the view is refreshed (that's healthy and accepted in my case!)
My problem is:
If the user clicked on tab2 while tab1 is loading, the user expects to see the contents of tab2 and ignore the first click on tab1.
What happens is, the contents of tab1 appears first then the contents of tab2. I want to prevent this behavior.
My framework contains of: Ruby 1.8.7 - Rails 2.3.5 - jQuery 1.5.2 (which is use to do AJAX things)
I tried to use some code like that but it didn't work with me
function doRequest(){
if (lastPageAjaxRequest){
lastPageAjaxRequest.abort();
lastPageAjaxRequest = null;
}
lastPageAjaxRequest = $.ajax(..);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是取消请求,而是取消成功回调。您可以在成功回调中进行检查,看看这是否是最近的请求,如果是,则更新页面,否则等待最近的请求返回。
Instead of cancelling the request, just cancel the success callback. You could have a check inside the success callback that looks to see if this is the most recent request, and if so, update the page, otherwise wait for the most recent request to come back.