gpt4 book ai didi

jquery - 在 DOMContentLoaded 之后,在 jQuery 中进行脚本异步调用的正确方法是什么?

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

我正在尝试找出重写一些代码的最佳方法。基本上,我们有一些广告在页面加载后通过 AJAX 加载(优先级不高,因此我们不希望它阻止 DOM 渲染)。这是我在 Firefox 的网络工具中看到的:

enter image description here

这是当前代码:

$(document).ready( function() {
....
$.getJSON("/cgi-bin/links/spots_load_new.cgi", {
catid: category_id,
linkid: link_id,
t: temp_set
}, function(results) {
showSpots(results);
});

});

...这就是我在阅读 $.when() - https://api.jquery.com/jquery.when/ 后认为可行的方法。 :

$(document).ready( function() {
....
$.when(
$.getJSON("/cgi-bin/links/spots_load_new.cgi", {
catid: category_id,
linkid: link_id,
t: temp_set,
})
).then( function(results) {
showSpots(results);
});
});

但是,这些项目似乎仍然在 DOMContentReady 之前调用。如何解决这个问题?

最佳答案

你的两个代码的作用完全相同。

您需要使用 $(document).ready 包装代码,如下所示:

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

关于jquery - 在 DOMContentLoaded 之后,在 jQuery 中进行脚本异步调用的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48426465/

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