开发者

Multiple $(document).ready functions [duplicate]

This question already has answers here: jQue开发者_如何学Gory - multiple $(document).ready ...? (6 answers) Closed 9 years ago.

If I have multiple $(document).ready(...) functions, do they overwrite each other? For the sake of argument, pretend proper coding is thrown out the door on this one.

Say I have a $(document).ready(function() {...}); in my site's script file. Then I use a third party plugin that also uses $(document).ready(function() {...});. Will this overwrite my already created function or does jQuery "queue" these functions to all run when the document is ready?


No, they do not override each other. Each function is executed.

You could of course check this easily yourself: http://jsfiddle.net/6jgGt/

Or understand from the jQuery code itself:

Line 255 is the ready function where the jQuery.bindReady(); is called which among other things initialises the readyList object on line 429 with readyList = jQuery._Deferred();

And once it's a deferred object the function passed in is appended with readyList.done( fn ); and we can see in the done method on line 41 that the element is added to an array with callbacks.push( elem ); so each one is saved separately...


No, they don't overwrite each other. They are queued, like you said.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜