gpt4 book ai didi

jquery - domReady 似乎在 RequireJS 中启动较晚

转载 作者:行者123 更新时间:2023-12-01 03:14:19 25 4
gpt4 key购买 nike

在我的应用程序中,当我使用 require (来自 RequireJS)时, domReady 事件会触发实际上是加载事件的事件(当所有图像、脚本等完成加载时)。这是预期的吗?

我有一堆使用 document.ready (来自 jQuery)的代码,但它无法正常运行,因为 domReady 事件触发得很晚。我编写了一个示例脚本来显示我面临的问题。

<html>
<head>
</head>
<body>
<script type="text/javascript" src="http://ratel.apache-extras.org.codespot.com/svn-history/r6/trunk/ratel/web/js/lib/require.js"></script>
<script>
require.config({
paths: {
"jquery": '//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min',
"jquery.validate": '//ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min'
},
shim: {
"jquery.validate": ["jquery"]
}
});

require(["jquery"], function (){
$(document).ready(function(){
console.log("the document is ready");
});
});
</script>
<img src="http://ie.microsoft.com/testdrive/HTML5/DOMContentLoaded/whidbey.jpg" width="300" height="300">
</body>
</html>

此 HTML 示例加载一个非常大的图像。我希望 $(document).ready 中的代码会在加载 jQuery 后立即触发。但它仅在大图像下载完成后才会触发。

我在这里做错了什么?

最佳答案

使用以下jsfiddle test ,这会将监听器附加到 window.DOMContentLoaded , window.load , $()require["domReady!] :

<script>
// IE patch
if (Function.prototype.bind && window.console && typeof console.log == "object") {
["log","info","warn","error","assert","dir","clear","profile","profileEnd"].forEach(function (method) {
console[method] = this.bind(console[method], console);
}, Function.prototype.call);
}

startTime = +new Date();
require = {
paths: {
"domReady": "https://rawgithub.com/requirejs/domReady/2.0.1/domReady",
"jquery": "http://code.jquery.com/jquery-1.10.2.min"
},
waitSeconds: 60
};
</script>
<script src="http://requirejs.org/docs/release/2.1.8/minified/require.js"></script>
<script>
function log() {
var args = Array.prototype.slice.apply(arguments);
var evt = args[0];
var type = evt && evt.type;
args = [+new Date() - startTime + "ms", type].concat(args);
console.log.apply(console, args);
}

require(["jquery"], function($) {
$(log("jquery.ready"));
});
require(["domReady!"], log);
window.addEventListener('load', log, false);
window.addEventListener('DOMContentLoaded', log, false);
</script>

<img id="theImg" src="http://ie.microsoft.com/testdrive/HTML5/DOMContentLoaded/whidbey.jpg" width="300" height="300">

<script>
var theImg = document.getElementById("theImg");
log("I am the trailing script and this is the image", theImg, theImg.id, theImg.parentNode, theImg.nextSibling);
</script>

我得到这些结果(FF 23、Chrome 29、IE 9):

FF 清理缓存。

[22:08:47.477] "488ms" undefined "I am the trailing script and this is the image" [object HTMLImageElement] "theImg" [object HTMLBodyElement] [object Text]
[22:08:47.478] "489ms" "DOMContentLoaded" [object Event]
[22:08:47.838] "849ms" undefined "jquery.ready"
[22:09:00.770] "13781ms" "load" [object Event]
[22:09:00.773] "13783ms" undefined [object HTMLDocument]

FF 启动缓存。

[22:09:19.881] "3ms" undefined "I am the trailing script and this is the image" [object HTMLImageElement] "theImg" [object HTMLBodyElement] [object Text]
[22:09:19.881] "3ms" "DOMContentLoaded" [object Event]
[22:09:19.883] "5ms" "load" [object Event]
[22:09:19.908] "30ms" undefined "jquery.ready"
[22:09:20.176] "298ms" undefined [object HTMLDocument]

Chrome 清理缓存。

745ms undefined I am the trailing script and this is the image <img id=?"theImg" src=?"http:?/?/?ie.microsoft.com/?testdrive/?HTML5/?DOMContentLoaded/?whidbey.jpg" width=?"300" height=?"300">? theImg <body>?…?</body>? #text
751ms DOMContentLoaded Event {clipboardData: undefined, cancelBubble: false, returnValue: true, srcElement: document, defaultPrevented: false…}
2024ms undefined jquery.ready fiddle.jshell.net/fiddlegrimbo/QrUxr/17/show/:53
16105ms load Event {clipboardData: undefined, cancelBubble: false, returnValue: true, srcElement: document, defaultPrevented: false…}
16127ms undefined #document

Chrome 启动缓存。

503ms undefined I am the trailing script and this is the image <img id=?"theImg" src=?"http:?/?/?ie.microsoft.com/?testdrive/?HTML5/?DOMContentLoaded/?whidbey.jpg" width=?"300" height=?"300">? theImg <body>?…?</body>? #text
508ms DOMContentLoaded Event {clipboardData: undefined, cancelBubble: false, returnValue: true, srcElement: document, defaultPrevented: false…}
1211ms undefined jquery.ready fiddle.jshell.net/fiddlegrimbo/QrUxr/17/show/:53
14952ms load Event {clipboardData: undefined, cancelBubble: false, returnValue: true, srcElement: document, defaultPrevented: false…}
14953ms undefined #document

IE 清理缓存。

LOG: 363msundefinedI am the trailing script and this is the image[object HTMLImageElement]theImg[object HTMLBodyElement][object Text] 
LOG: 364msDOMContentLoaded[object Event]
LOG: 1171msundefinedjquery.ready
LOG: 13743msload[object Event]
LOG: 13747msundefined[object Document]

IE 启动缓存。

LOG: 6msundefinedI am the trailing script and this is the image[object HTMLImageElement]theImg[object HTMLBodyElement][object Text] 
LOG: 126msDOMContentLoaded[object Event]
LOG: 128msload[object Event]
LOG: 164msundefinedjquery.ready
LOG: 416msundefined[object Document]

因此,当缓存干净时,FF、Chrome 和 IE 中的回调顺序是相同的:

  • DOMContent已加载
  • jquery.ready
  • 窗口加载
  • dom准备好了!

但是当缓存启动时,IE 的顺序会发生变化。 FF 和 Chrome 保持不变。

而且 FF 和 IE 似乎第二次运行得快得多。尽管缓存已准备就绪,但 Chrome 的延迟似乎大致相同。

如果有必要,您必须执行自己的测试,因为我只运行了几次。所以我并不是说这是一致的行为(尽管可能是)。

忽略ready/load/$()/domReady!等等

您当然可以忽略所有这些回调,只需在 </body> 内的页面末尾添加一个脚本 block 即可。标签。脚本 block 上方的所有 DOM 元素都应可用于某些操作。同样,您必须测试您的特定页面/网站。

关于jquery - domReady 似乎在 RequireJS 中启动较晚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18690831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com