浏览器在收到响应时会导致 JS 中断吗?
假设,无论出于何种原因,我们在一个页面上有三个内联脚本。这些脚本中的每一个在功能上都是相同的,并且所有三个脚本都会创建一个脚本标记,并将源设置为服务器端脚本(每个脚本的参数略有不同),该脚本返回要执行的 JS。假设这个 JS 包含一个全局变量,其中包含它最后调用的函数的重要参数。
var g_important = {"arbitrary1":"someval1", "arbitrary2":"someval2"};
doSomething();
有问题的脚本驻留在带有负载均衡器的 CDN 上。假设来自我们原始 JS 的每个请求实际上都可以发送到不同的服务器。服务器的响应时间现在是可变的,这意味着我们可能首先得到对第二个请求的响应。这不是问题,因为我们随代码一起发送回参考值,确保我们至少正在处理指定的对象。
我的问题是:当我们几乎同时收到两个回复时会发生什么?这是一种非常不可能的情况,但是如果当第二个响应到来时第一个响应正在执行 doSomething() 过程中,浏览器会做什么?它是否解析变量的值并将第二个 doSomething 调用推入堆栈?
假设情况确实如此,理论上 doSomething 可能会使用错误的值集。。我无法重现这个案例,因为时机必须绝对完美,但我需要知道这是否是一个问题。
Assume, for whatever reason, that we have three inline scripts on a page. Each of these scripts are functionally the same, and all three will create a script tag, with the source set to a server-side script (each with slightly different parameters) which returns JS to execute. Assume this JS contains a global variable containing important parameters for the function it calls at the end.
var g_important = {"arbitrary1":"someval1", "arbitrary2":"someval2"};
doSomething();
The script in question resides on a CDN with a load balancer. Assume that each request from our original JS could actually be sent to a different server. The response time from the server is now variable, meaning we might get our response to the second request first. That's not a problem because we send a reference value back with the code, ensuring that we're at least working with the specified object.
My question is: what happens when we get two responses at roughly the same time? This is a very unlikely case, but if the first response is in the process of executing doSomething() when the second response comes in, what does the browser do? Does it parse the value for the variable and push the second doSomething call onto the stack?
Assuming this is the case, doSomething could be working with the wrong set of values in theory. I can't recreate this case, as the timing would have to be absolutely perfect, but I need to know if it's a problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Javascript“一次运行一行”。如果另一个脚本正在运行,脚本将始终被堆叠。
从技术上讲,2 个脚本不能同时加载,因此 1 个脚本会先于另一个脚本执行。
Javascript operates "one line at a time". Scripts will always be stacked if another script is running.
And technically, 2 scripts cannot be loaded at the same time, so 1 will be executed before the other.
这取决于如何注入脚本。成功确保执行顺序的技术各不相同。常见且简单的
document.createElement
+head.appendChild
方式不会例如,确保 IE 中的顺序。
更多信息位于 不阻塞地加载脚本
It depends on how you inject the script. Techniques vary in success of ensuring the order of execution. The common and simple
document.createElement
+head.appendChild
way will notensure order in IE for example.
more info at Load script without blocking
那么您担心
g_important
对象在执行doSomething()
期间可能会发生变化?我认为这是不可能的,但如果您担心这一点,为什么不注册doSomething()
作为每个 AJAX 请求完成时的回调。然后,对该函数的每次调用都会传递其请求的数据,而不会传递其他数据。也许我不太了解情况,但这种事情似乎是很容易避免的,如果有可能的话。
So you're worried that the
g_important
object could change during the execution ofdoSomething()
? I don't think this is possible, but if you're worried about it, why not registerdoSomething()
as a callback on the completion of each AJAX request. Then each call to the function gets passed the data for its request and never the others.Maybe I don't fully understand the situation, but this kind of thing would seem to be easily avoidable, if its even possible.