不建议使用同步XMLHttpRequest [英] Synchronous XMLHttpRequest deprecated

查看:376
本文介绍了不建议使用同步XMLHttpRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,由于扩展程序出现问题,我不得不重新启动浏览器。重新启动后发现,我的浏览器(Chromium)自动更新到了不再允许同步AJAX请求的新版本。 Quote:

Today, I had to restart my browser due to some issue with an extension. What I found when I restarted it, was that my browser (Chromium) automatically updated to a new version that doesn't allow synchronous AJAX-requests anymore. Quote:


不推荐使用主线程上的同步XMLHttpRequest,因为
对最终用户的体验有不利影响。要获得更多帮助,
请检查 http://xhr.spec.whatwg.org/

我需要同步AJAX请求使我的node.js应用程序正常工作,因为它们通过服务器存储并从磁盘加载数据利用fopen。我发现这是一种非常简单有效的处理方式,在创建小型业余项目和编辑器时非常方便...是否有办法在Chrome / Chromium中重新启用同步XMLHttpRequests?

I need synchronous AJAX-requests for my node.js applications to work though, as they store and load data from disk through a server utilizing fopen. I found this to be a very simplistic and effective way of doing things, very handy in the creation of little hobby projects and editors... Is there a way to re-enable synchronous XMLHttpRequests in Chrome/Chromium?

推荐答案

此答案已被编辑。

简短答案:
他们不想在 main 线程上进行同步。

对于支持线程/网络工作者的新浏览器而言,该解决方案很简单:

The solution is simple for new browsers that support threads/web workers:

var foo = new Worker("scriptWithSyncRequests.js")

在工作线程中既不会显示DOM也不会显示全局变量,但是封装多个同步请求将非常容易。

Neither DOM nor global vairables aren't going to be visible within a worker but encapsulation of multiple synchronous requests is going to be really easy.

另一种解决方案是切换到异步但使用浏览器 localStorage 以及JSON.stringify作为媒介。如果允许执行某些IO,则可以模拟localStorage。
http://caniuse.com/#search=localstorage

Alternative solution is to switch to async but to use browser localStorage along with JSON.stringify as a medium. You might be able to mock localStorage if you allowed to do some IO. http://caniuse.com/#search=localstorage

只是为了好玩,如果我们只想使用同步来限制自己的自我,还有其他方法:

Just for fun, there are alternative hacks if we want to restrict our self using only sync:

使用setTimeout很诱人因为可能会认为这是将同步请求封装在一起的一种好方法。可悲的是,有一个陷阱。 javascript中的异步并不意味着它可以在自己的线程中运行。异步可能会推迟呼叫,等待其他人结束。对我们来说幸运的是,隧道尽头有光,因为您很可能可以同时使用xhttp.timeout和xhttp.ontimeout进行恢复。请参见超时XMLHttpRequest
这意味着我们可以实现时间表的小版本,该时间表可以处理失败的请求并分配时间再试一次或报告错误。

It is tempting to use setTimeout because one might think it is a good way to encapsulate synchronous requests together. Sadly, there is a gotcha. Async in javascript doesn't mean it gets to run in its own thread. Async is likely postponing the call, waiting for others to finish. Lucky for us there is light at the end of the tunnel because it is likely you can use xhttp.timeout along with xhttp.ontimeout to recover. See Timeout XMLHttpRequest This means we can implement tiny version of a schedular that handles failed request and allocates time to try again or report error.

// The basic idea.
function runSchedular(s)
{
    setTimeout(function() {
        if (s.ptr < callQueue.length) {
            // Handles rescheduling if needed by pushing the que.
            // Remember to set time for xhttp.timeout.
            // Use xhttp.ontimeout to set default return value for failure.
            // The pushed function might do something like: (in pesudo)
            // if !d1
            // d1 = get(http...?query);
            // if !d2
            // d2 = get(http...?query);
            // if (!d1) {pushQue tryAgainLater}
            // if (!d2) {pushQue tryAgainLater}
            // if (d1 && d2) {pushQue handleData}
            s = s.callQueue[s.ptr++](s);
        } else {
            // Clear the que when there is nothing more to do.
            s.ptr = 0;
            s.callQueue = [];
            // You could implement an idle counter and increase this value to free
            // CPU time.
            s.t = 200;
        }
        runSchedular(s);
    }, s.t);
}

这篇关于不建议使用同步XMLHttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆