简体   繁体   English

有100个文件准备好了还是比1个文件准备好了?

[英]Is having 100 document ready better or worse than 1 document ready?

Just wondering if the amount of document.ready calls affects page load speed. 只是想知道document.ready调用的数量是否会影响页面加载速度。 Is there a way in Gulp / Grunt to uglify / minify JS by removing seperate document ready functions? Gulp / Grunt有没有办法通过删除单独的文档就绪函数来丑化/缩小JS?

Just check it! 检查一下!

I don't see significant difference in Chrome. 我认为Chrome没有显着差异。
As I know, it was critical for IE8, but didn't check this fact. 据我所知,它对IE8至关重要,但没有检查这个事实。
IE11 shows 2 seconds on the first snippet, when the others take 200 ms only. IE11在第一个片段上显示2秒,而其他片段仅显示200毫秒。

Also, seems like jQuery already aggregates load events. 此外,似乎jQuery已经聚合加载事件。

Don't forget 别忘了

  1. When you are running same code in one tab, browser remembers something and runs it faster. 当您在一个选项卡中运行相同的代码时,浏览器会记住某些内容并更快地运行它。
  2. Reload the page is not enought. 重新加载页面是不应该的。 Open a new tab instead. 改为打开一个新标签。
  3. After opening a new tab, run snippets in different order. 打开新选项卡后,以不同的顺序运行代码段。
  4. If the snippet is ran first on the tab, it will get additional slowdown, comparing the other three. 如果首先在选项卡上运行代码段,则会比其他三个更慢。

 for (var q=0; q<1000; ++q) { document.addEventListener('DOMContentLoaded', (function (i) { console.log(i); }).bind(null, q)); } document.addEventListener('DOMContentLoaded', function () { document.querySelector('output').textContent = performance.now().toFixed(3); }); 
 <output></output> 

 document.addEventListener('DOMContentLoaded', function () { for (var q=0; q<1000; ++q) { (function (i) { console.log(i) }).bind(null, q)(); document.querySelector('output').textContent = performance.now().toFixed(3); } }); 
 <output></output> 

 for (var q=0; q<1000; ++q) { $((function (i) { console.log(i); }).bind(null, q)); } $(function () { document.querySelector('output').textContent = performance.now().toFixed(3); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <output></output> 

 $(function () { for (var q=0; q<1000; ++q) { (function (i) { console.log(i) }).bind(null, q)(); document.querySelector('output').textContent = performance.now().toFixed(3); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <output></output> 

Maybe it's just me as a JavaScript avoider, but none of the scripts have document.ready inside. 也许只是我作为一个JavaScript避免者,但没有一个脚本有document.ready里面。 If you JS guys talk about document.ready, that's a synonym for addEventListener('DOMContentLoaded') ? 如果你们JS的人谈论document.ready,那就是addEventListener('DOMContentLoaded')的同义词?

There are two events: DOMContentLoaded and load ( window.onload ). 有两个事件: DOMContentLoadedloadwindow.onload )。 First of them occures when the body pasring is complete, but some assets are loading still. 当身体完成时,第一个发生,但一些资产仍在加载。 The second - when the page is completely loaded. 第二个 - 页面完全加载。 First one is nice for running scripts with dom manipulations, but browsers not always had support of it. 第一个很适合使用dom操作运行脚本,但浏览器并不总是支持它。

So jQuery uses the first of these two events and classic form of subscription was 所以jQuery使用这两个事件中的第一个和经典的订阅形式

$(document).ready(function () {
  // ...
});

but after some versions if was simplified to passing function directly into jQuery: 但是在一些版本之后如果简化为直接将函数传递给jQuery:

$(function () {
  // ...
});

So in vanilla examples I'm using the first of 2 events, and in jQuery examples I'm using the short form of subscription on it. 所以在vanilla示例中,我使用了2个事件中的第一个,在jQuery示例中,我使用了简短的订阅形式。 As browsers without support of this event are very old it's correct to assume that jQuery always uses DOMContentLoaded (probably the load way is removed in version 2 - didn't check it, but see no reasons to keep it there). 由于不支持此事件的浏览器非常陈旧,因此假设jQuery始终使用DOMContentLoaded是正确的(可能在版本2中删除了load方式 - 没有检查它,但是没有理由将其保留在那里)。

Many document ready calls shouldn't affect much the application performance. 许多文档就绪调用不应该影响应用程序性能。 The best solution may be having only one and init there all you need. 最好的解决方案可能只有一个,并在那里初始化所需的一切。 But it depends on your application structure and you should be more confortable having more than one. 但这取决于你的应用程序结构,你应该更安心,不止一个。 Anyway, I don't think there is any Gulp task that wraps different ready functions in one, because it will touch the application logic. 无论如何,我认为没有任何Gulp任务将不同的就绪函数包含在一个中,因为它将触及应用程序逻辑。

You can have multiple ones, but it's not always the neatest thing to do. 你可以有多个,但并不总是最好的事情。 Try not to overuse them, as it will seriously affect readability. 尽量不要过度使用它们,因为它会严重影响可读性。 Other than that , it's perfectly legal. 除此之外,它是完全合法的。

It's also worth noting that a function defined within one $(document).ready block cannot be called from another $(document).ready block. 还值得注意的是,在一个$(document).ready块中定义的函数不能从另一个$(document).ready块调用。

$(document).ready(function() {
    alert('hello1');
    function saySomething() {
        alert('something');
    }
    saySomething();

});
$(document).ready(function() {
    alert('hello2');
    saySomething();
});

output was 输出是

hello1
something
hello2

Check this post and this one 查看这篇文章这篇 文章

Yes, you can use multiple document ready handler, there is no special advantage even though you can use jQuery code in several place. 是的,你可以使用多个文件就绪处理程序,即使你可以在几个地方使用jQuery代码也没有特别的优势。 You can't use the variable inside one in another since those are in different scope. 您不能将变量放在另一个变量中,因为它们位于不同的范围内。

Actually jQuery event handler pushing function for execution in queue of a particular event. 实际上jQuery事件处理程序推送函数在特定事件的队列中执行。 When event is fired all functions executes one by one from particular events row/stack/queue based on return value of parent sequential function. 当事件被触发时,所有函数基于父顺序函数的返回值从特定事件行/堆栈/队列逐个执行。
BUT There is one thing to note that each $(document).ready() function call must return. 有一点需要注意,每个$(document).ready()函数调用必须返回。 If an exception is thrown in one, subsequent calls will never be run. 如果在一个异常中抛出异常,则永远不会运行后续调用。

 $(document).ready(function() { document.write('<h3>In First ready function</h3>'); var foo = function() { console.log('inside foo'); } document.write("foo:" +(typeof foo)+"<br>"); document.write("bar:" +(typeof bar)+"<br>"); }); $(document).ready(function() { document.write('<h3>In Second ready function</h3>'); var bar=function bar() { console.log('inside bar'); } document.write("foo:" +(typeof foo)+"<br>"); document.write("bar:" +(typeof bar)+"<br>"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Actually jQuery $(document).ready() method is attach function with DOMContentLoaded event using addEventListener method. 实际上jQuery $(document).ready()方法是使用addEventListener方法附加DOMContentLoaded事件的函数。

Yes you can have multiple instance of it on a single page. 是的,您可以在一个页面上拥有多个实例。 There is no particular advantage. 没有特别的优势。 All will get executed on first called first run basis. 所有这些都将在首次调用首次运行时执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM