在Chrome& IE,首先显示“同步ajax调用:成功”,
但是在FF(在FF 3.6和FF 17.0中测试)中,首先显示“async ajax call:success”,
这意味着虽然我们将第二个作为同步调用,但是当它的onreadystatechange被触发时,异步(第一个)ajax调用的处理程序比同步(第二个)ajax调用执行得更早,它是否有意义?
这不是一个火狐虫吗?
// first ajax call,Note: this is asynchronous. $.ajax({ url: "/rest/someUrl",async : true,dataType : "json",contentType: "application/json",success : function(data) { console.log("async ajax call: success"); },error : function(data) { } }) // second ajax call,Note: this is synchronous. $.ajax({ url: "/rest/someUrl",async : false,success : function(data) { console.log("sync ajax call: success"); },error : function(data) { } })
解决方法
在规范中,我没有发现任何引用,只要同步请求没有完成,所有脚本都应该停止执行(请注意,当sync-XHR启动时,async-XHR已经在运行).
但我发现了这个:
> Each XMLHttpRequest object has its own task source. Namely,the XMLHttpRequest task source.
– 两个请求代表一个任务源 –
> When a user agent is to queue a task,it must add the given task to one of the task queues of the relevant event loop. […] tasks from different task sources may be placed in different task queues.
– 任何任务都可以添加到同一个任务队列中,但不能 –
> An event loop must continually run through the following steps for as long as it exists:
1.Run the oldest task on one of the event loop’s task queues,if any,[…]. The user agent may pick any task queue.
– 他现在选择他在同步请求上放置的任务队列
当我没有误解这一点,并且我的逻辑没有错时,这可能会继续:
Firefox将两个XHR放在同一个队列中,IE和chrome将它们放在不同的任务队列中.
所有浏览器现在都运行任务队列,放置同步XHR.
>在IE和chrome中,同步XHR是其队列中最早的任务并运行
>在FF中,异步XHR是队列中最早的并且运行
两种实现似乎都是正确的.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。