为什么jQuery异步触发就绪事件

时间:2022-11-13 19:59:15

In jQuery ready event logic:

jQuery就绪事件逻辑:

// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
if ( document.readyState === "complete" ) {
    // Handle it asynchronously to allow scripts the opportunity to delay ready
    return setTimeout( jQuery.ready, 1 );
}

Could you explain the comment: "Handle it asynchronously to allow scripts the opportunity to delay ready".

你能解释一下这个评论吗:“异步处理它,让脚本有机会延迟。”

I don't understand what scripts and why to delay ready?

我不明白是什么剧本,为什么要拖延?

2 个解决方案

#1


6  

If the ready callback(which fires the readyList) would have fire right away, you couldn't hold it's execution once the DOM is ready with the holdReady function.

如果ready回调(它触发readyList)将立即触发,那么在DOM准备好使用holdReady函数之后,就无法保持它的执行。

jQuery.holdReady( hold )
Description: Holds or releases the execution of jQuery's ready event.

jQuery。保持(保持)描述:保持或释放jQuery就绪事件的执行。

The $.holdReady() method allows the caller to delay jQuery's ready event.

$. holdready()方法允许调用者延迟jQuery的就绪事件。

This advanced feature would typically be used by dynamic script loaders that want to load additional JavaScript such as jQuery plugins before allowing the ready event to occur, even though the DOM may be ready.

这种高级特性通常会被动态脚本加载器使用,在允许准备事件发生之前,需要加载额外的JavaScript(如jQuery插件),即使DOM可能已经准备好了。

This method must be called early in the document, such as in the immediately after the jQuery script tag. Calling this method after the ready event has already fired will have no effect.

这个方法必须在文档的早期调用,比如在jQuery脚本标记之后。在就绪事件已经触发后调用此方法将无效。

#2


0  

it has to do with $.holdReady(), which allows you to delay the ready event. A use case might be that you want to do some initialization stuff before all of your other scripts run. You could delay ready until you are done initializing.

它与$. holdready()有关,它允许您延迟就绪事件。一个用例可能是您希望在所有其他脚本运行之前完成一些初始化工作。您可以延迟,直到您完成初始化。

#1


6  

If the ready callback(which fires the readyList) would have fire right away, you couldn't hold it's execution once the DOM is ready with the holdReady function.

如果ready回调(它触发readyList)将立即触发,那么在DOM准备好使用holdReady函数之后,就无法保持它的执行。

jQuery.holdReady( hold )
Description: Holds or releases the execution of jQuery's ready event.

jQuery。保持(保持)描述:保持或释放jQuery就绪事件的执行。

The $.holdReady() method allows the caller to delay jQuery's ready event.

$. holdready()方法允许调用者延迟jQuery的就绪事件。

This advanced feature would typically be used by dynamic script loaders that want to load additional JavaScript such as jQuery plugins before allowing the ready event to occur, even though the DOM may be ready.

这种高级特性通常会被动态脚本加载器使用,在允许准备事件发生之前,需要加载额外的JavaScript(如jQuery插件),即使DOM可能已经准备好了。

This method must be called early in the document, such as in the immediately after the jQuery script tag. Calling this method after the ready event has already fired will have no effect.

这个方法必须在文档的早期调用,比如在jQuery脚本标记之后。在就绪事件已经触发后调用此方法将无效。

#2


0  

it has to do with $.holdReady(), which allows you to delay the ready event. A use case might be that you want to do some initialization stuff before all of your other scripts run. You could delay ready until you are done initializing.

它与$. holdready()有关,它允许您延迟就绪事件。一个用例可能是您希望在所有其他脚本运行之前完成一些初始化工作。您可以延迟,直到您完成初始化。