- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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/
我是一名优秀的程序员,十分优秀!