Effect.toggle 和 AJAX 调用
我使用以下代码来捕获从我的应用程序发出的所有 AJAX 调用,以便在调用完成之前显示繁忙的旋转器。当请求间隔良好时,它会很好地发挥作用。但是,当快速连续发出请求时,某些 AJAX 调用不会注册,或者 onCreate 和 onComplete 操作同时启动,并且通常情况下,繁忙的旋转器继续出现在屏幕上,在所有调用成功完成后。我可以在调用结束时执行任何检查,以检查该元素是否可见,我可以隐藏它。
document.observe("dom:loaded", function() {
$('loading').hide();
Ajax.Responders.register({
//When an Ajax call is made.
onCreate: function() {
new Effect.toggle('loading', 'appear');
new Effect.Opacity('display-area', { from: 1.0, to: 0.3, duration: 0.7 });
},
onComplete: function() {
new Effect.toggle('loading', 'appear');
new Effect.Opacity('display-area', { from: 0.3, to: 1, duration: 0.7 });
}
});
});
谢谢!
I am using the following code to capture all the AJAX calls being made from my app, in order to show a busy spinner till the call is completed. It works well when the requests are well spaced out. However, when the request are made in quick successions, some of the AJAX calls do not get registered, or the onCreate and onComplete actions get kicked off at the same time, and more often than not, the busy spinner continues to appear on the screen, after all the calls have successfully completed. Is there any check I can perform at the end of the call, to check if the element is visible, I can hide it.
document.observe("dom:loaded", function() {
$('loading').hide();
Ajax.Responders.register({
//When an Ajax call is made.
onCreate: function() {
new Effect.toggle('loading', 'appear');
new Effect.Opacity('display-area', { from: 1.0, to: 0.3, duration: 0.7 });
},
onComplete: function() {
new Effect.toggle('loading', 'appear');
new Effect.Opacity('display-area', { from: 0.3, to: 1, duration: 0.7 });
}
});
});
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要记录正在处理的 AJAX 请求数,并且仅在没有其他请求正在处理时才隐藏加载指示器。
尝试类似以下的代码:
You need to keep a count of how many AJAX requests are processing, and only hide the loading indicator if no further requests are processing.
Try code similar to the following:
一种简单的方法是创建一个计数器变量,该变量的作用域超出了两个函数的范围,并在 onCreate 中递增它并在 onComplete 中递减。然后仅当计数器达到 0 时才隐藏微调器。
A simple approach would be to create a counter variable that has scope outside your two functions and increment it in onCreate and decrement in onComplete. Then only hide the spinner when the counter hits 0.