加载 gif“冻结”当代码执行时
我正在附加一个 gif 动画,它是一个加载栏。 gif 是在代码的前面定义的,然后我只需执行以下操作..ajax
document.getElementById("loading").style.display = "inline";
//some if statements.
do_ajax(params);
调用看起来像...
var uri = getURL(params);
var xhr = new XMLHttpRequest();
xhr.open("POST",uri,false);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
xhr.send(null);
显示加载 gif。然后,我执行一些检查(if 语句),然后发出 AJAX 请求。然而,当所有这些代码执行时,gif 会冻结,并且只有在代码执行完毕后才开始“移动”。这是为什么呢?我该如何解决这个问题?
我使用的是 Chrome 11
I'm appending an animated gif which is a loading bar. The gif is defined earlier in the code and I then just do the following..
document.getElementById("loading").style.display = "inline";
//some if statements.
do_ajax(params);
The ajax call looks something like...
var uri = getURL(params);
var xhr = new XMLHttpRequest();
xhr.open("POST",uri,false);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
xhr.send(null);
to show the loading gif. I then perform some checks (if statements) followed by an AJAX request. However, the gif freezes while all this code is executing and only begins to 'move' once the code has finished executing. Why is this? How do I fix this issue?
I'm using Chrome 11
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用同步 XHR。这将在等待请求完成时锁定浏览器。显然,这会导致严重的 UI 问题。
尝试异步。将
open()
的第三个参数设置为true
。您还需要分配readystatechange
事件并侦听是否成功。进一步阅读。
You are using synchronous XHR. This will lock the browser whilst waiting for the request to finish. Obviously, it causes a serious UI issue.
Try asynchronous. Set
open()
's 3rd parameter totrue
. You also need to assign thereadystatechange
event and listen for success.Further Reading.
我想只要确保您使用的是 async: true
I guess just make sure you are using async: true
您是否希望在同步 ajax 请求运行时看到 loading.gif?
同步 ajax 请求会完全锁定大多数浏览器的 UI,从而阻止您所做的任何更改在请求完成之前显示。
如果您想在请求运行时查看loading.gif,请将其设为异步:
Are you expecting to see the loading.gif while your synchronous ajax request is running?
Synchronous ajax requests completely lock up the UI of most browsers, preventing any changes you make from being displayed until the request completes.
If you want to see loading.gif while the request is running, make it asynchronous: