开发者

jQuery Document Ready - Is it best to wrap each statement or just wrap all statements in one document.ready event?

开发者 https://www.devze.com 2023-04-06 01:50 出处:网络
I have seen it both ways, but which is better or does it not matter. I feel as though wrapping each statement might be cleaner, but just wondering if its more callbacks if you have 50 statements each

I have seen it both ways, but which is better or does it not matter.

I feel as though wrapping each statement might be cleaner, but just wondering if its more callbacks if you have 50 statements each with their own do开发者_StackOverflow中文版cument.ready event handler?


There is a temptation among jQuery developers to hook everything into the $(document).ready pseudo event. After all, it is used in most examples you will find.

Although $(document).ready is incredibly useful, it occurs during page render while objects are still downloading. If you notice your page stalling while loading, all those $(document).ready functions could be the reason why.

You can reduce CPU utilization during the page load by binding your jQuery functions to the $(window).load event, which occurs after all objects called by the HTML (including content) have downloaded.

Read more about jQuery performance and best practices here or watch this video

0

精彩评论

暂无评论...
验证码 换一张
取 消