Javascript Chrome 只是没有完成加载 JS 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25847083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chrome just doesn't finish loading JS files
提问by Kshitij Saxena -KJ-
I am writing an app that includes about 12 short JS files in the <head>section (I know, I should merge them and move them just before the end of <body>; would get to those when in production)
我正在编写一个应用程序,其中包含该<head>部分中大约 12 个简短的 JS 文件(我知道,我应该合并它们并在结束之前移动它们<body>;将在生产中使用这些文件)
The trouble is that when I try to load the app in Chrome, Some files load immediately while some never finish loading at all!Chrome keeps trying to load the 12 JS files and never renders the page until I hit "Stop".
问题是,当我尝试在 Chrome 中加载应用程序时,有些文件会立即加载,而有些文件根本就加载不完!Chrome 不断尝试加载 12 个 JS 文件,并且在我点击“停止”之前从不呈现页面。
When I hit stop, the HTML is rendered and the JS files fail as in the image below:
当我点击停止时,HTML 被渲染并且 JS 文件失败,如下图所示:


Note that different JS files fail on each attempt! It's not the same file that gets stuck every time!
请注意,每次尝试都会失败不同的 JS 文件!不是每次都卡住的同一个文件!
Inspecting the headers of the failed files shows "Caution: request is not finished yet". The files are stuck in "Receiving" sometimes for many minutes!
检查失败文件的标题显示“注意:请求尚未完成”。文件有时会卡在“接收”状态好几分钟!


Now here's the fun part, after hitting stop, if I focus on the omnibar and press enter, all the JS files load instantlyand the application works fine!
现在是有趣的部分,按下停止键后,如果我专注于多功能栏并按 Enter,所有 JS 文件都会立即加载并且应用程序运行正常!
On the server side, I am using Apache, PHP and MySQL. Have I misconfigured something in Apache?
在服务器端,我使用 Apache、PHP 和 MySQL。我在 Apache 中配置错误了吗?
STATUS after 2 gruelling days: zilch, nothing, nada, this is driving me nuts. I have tried running the code from different machines, have tried changing apache cache settings and changed myriad things in javascript but nothing has worked. The worst thing is that no one can pin point where the problem is!
2 天后的状态:zilch,没什么,nada,这让我发疯。我试过从不同的机器运行代码,试过更改 apache 缓存设置并在 javascript 中更改了无数的东西,但没有任何效果。最糟糕的是,没有人能指出问题出在哪里!
回答by Dave Morrissey
One possibility is that you have Keep-Alive enabled and it is not working properly. I've seen this before. The browser establishes it's maximum number of connections to your server (typically 6) to download the first few files (CSS, JS etc.) then those connections are not released until they time out. My symptoms were not quite the same as yours - the timeout was 20 seconds and everything would load in batches of 6 after that - but this could still be the cause.
一种可能性是您启用了 Keep-Alive,但它无法正常工作。我以前见过这个。浏览器建立到您的服务器的最大连接数(通常为 6 个)以下载前几个文件(CSS、JS 等),然后这些连接在超时之前不会释放。我的症状和你的不太一样——超时是 20 秒,之后所有东西都会分批加载 6 秒——但这仍然可能是原因。
In your Apache configuration (httpd.conf on most systems), look for the KeepAliveline (or add it if it's missing) and set it to Off.
在您的 Apache 配置(大多数系统上为 httpd.conf)中,查找该KeepAlive行(如果缺少则添加)并将其设置为Off.
回答by idrarig
More than an answer, here's how I would troubleshoot the problem.
这不仅仅是一个答案,这是我解决问题的方法。
One of the things I'd try is commenting out tags one at a time and reload, to see where the threshold is. Also, because this is probably a server configuration problem, I'd restart it after each try, to have a clean slate, so to speak, i.e., no state preserved between tries.
我会尝试的一件事是一次注释掉一个标签并重新加载,以查看阈值在哪里。另外,因为这可能是服务器配置问题,所以我会在每次尝试后重新启动它,以保持干净的状态,可以这么说,即在尝试之间不保留任何状态。
I'd try to get more hints by trying to make the requests for the various Javascripts from a script in your favourite language. Ideally, I'd try GETting the scripts one by one (say with curl) waiting a few milliseconds between requests. I imagine I'd hit a threshold here as well. Like, getting one script per second works, but when requests get too close, the server gets stuck.
我会尝试通过使用您喜欢的语言从脚本中发出对各种 Javascript 的请求来获得更多提示。理想情况下,我会尝试一个接一个地获取脚本(比如使用 curl),在请求之间等待几毫秒。我想我也会在这里达到一个门槛。比如,每秒获取一个脚本是可行的,但是当请求太接近时,服务器就会卡住。
If still no clue, I'd use tcpdump to watch the traffic between the browser and the server. Okay, this may be a little too low level!
如果仍然不知道,我会使用 tcpdump 来观察浏览器和服务器之间的流量。好吧,这可能有点太低级了!
But perhaps I'd use netstat to see how many connections the browser opens in parallel to the server to fetch the resources, and see if we hit a concurrency limit.
但也许我会使用 netstat 来查看浏览器与服务器并行打开了多少个连接以获取资源,并查看我们是否达到了并发限制。
I'm sorry this is a solution but I hope you get some ideas, and I'd be very curious to know what your problem is, in the end!
对不起,这是一个解决方案,但我希望你能得到一些想法,我很想知道你的问题是什么,最后!
回答by Jos de Jong
We got exactly the same message "Caution: request is not finished yet" after a request in the browser.
在浏览器中发出请求后,我们收到了完全相同的消息“注意:请求尚未完成”。
The request itself was in the order of 15 MB of JSON, which was fed into an Angular 1 application. This request took about 2 seconds. Right after the request was finished, the Angular digest cycle blocked the browser for more than 12 seconds (this is visible by profiling during the request). During that time Chrome showed this "Caution: request is not finished yet" message in request whilst it actually was already finished.
请求本身大约为 15 MB 的 JSON,它被输入到 Angular 1 应用程序中。这个请求用了大约 2 秒。请求完成后,Angular 摘要循环阻塞了浏览器超过 12 秒(通过请求期间的分析可以看到这一点)。在此期间,Chrome 在请求中显示此“警告:请求尚未完成”消息,而实际上它已经完成。
回答by Kondrat Shmoylov
Try to emulate problem in https://www.stevesouders.com/cuzillion/It should say you side of problem - chrome or server
尝试在https://www.stevesouders.com/cuzillion/ 中模拟问题 它应该说你是问题的一方 - chrome 或服务器
回答by Viacheslav Gostiukhin
Check the Content-Length header. Server may pass incorrect value - greather than actual content length.
检查 Content-Length 标头。服务器可能传递不正确的值 - 大于实际内容长度。
回答by Ramsay Smith
There are several good answers, but if you want a foolproof method of doing this, you can use PHP to send all the scripts at once. If the problem is truly too many connections to the server, you could add some html code like this:
有几个很好的答案,但是如果您想要一种万无一失的方法,您可以使用 PHP 一次发送所有脚本。如果问题确实是到服务器的连接太多,您可以添加一些这样的 html 代码:
<script type="text/javascript" src="scripts/scripts.php />
And then in scripts.phpyou use the include()function to include all of the JavaScript files like so:
然后在scripts.php您使用该include()函数来包含所有 JavaScript 文件,如下所示:
<?php
header(''); // control all your headers
include('jquery-1.9.1.min.js');
// rest of scripts
?>
If nothing else works and the problem is excessive connections, this should work.
如果没有其他任何工作并且问题是连接过多,则这应该有效。

