gpt4 book ai didi

jquery 获取/getJSON : call a function after *several* files load?

转载 作者:行者123 更新时间:2023-12-01 01:18:34 27 4
gpt4 key购买 nike

我有一个 jquery 页面,它加载存储在多个外部文件中的部件。我需要执行一个函数,但只有在触发所有 get 和 getJSON 回调之后。

有什么好的方法吗?

目前,我将所有 get 命令嵌套在一起。代码很丑陋,而且更糟糕的是,gets 被依次执行,导致页面加载速度变慢。

最佳答案

如果您使用的是 >= jQuery 1.5,则可以使用 $.when() :

$.when($.getJSON('/test1'), $.getJSON('/test2')).done(function(result1, result2) {
console.log(result1[0]); //result1 is an array of the response values from test1
console.log(result2[0]);
});

.done 函数仅在所有 ajax 调用成功后才会执行。还有一个 .then 方法可以获取成功和失败函数。

关于jquery 获取/getJSON : call a function after *several* files load?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7940835/

27 4 0
文章推荐: jquery - Ajax 调用 Web 服务失败
文章推荐: ajax - Laravel 中的 PATCH AJAX 请求
文章推荐: Nginx 配置错误 : can not detect int size
文章推荐: jquery - 单击时内联编辑 ,Rails 3.0/jQuery AJAX