【发布时间】:2021-06-25 01:03:26
【问题描述】:
我在 JavaScript 中有两个 ajax 调用,它们以异步方式向页面添加内容。我需要创建一个“超时”函数(或类似的东西),等待它们都完成加载,然后执行更多代码。这是我目前所拥有的
https://jsfiddle.net/qcu5asnj/
<div class='resultsSection'>
Example
</div>
<script>
var loading_first = "loading";
var first_ajax = {
url: 'https://example.com/load.php?q=fast',
type: "GET",
success: function( data ) {
console.log("success");
$(".resultsSection").append(data);
loading_first = "done";
}
};
$.ajax( first_ajax );
var loading_second = "loading";
var second_ajax = {
url: 'https://example.com/load.php?q=slow',
type: "GET",
success: function( data ) {
console.log("success");
$(".resultsSection").append(data);
loading_second = "done";
}
};
$.ajax( second_ajax );
// Need to create a function that waits until both are done. I know this is wrong
if((loading_first == "done") && (loading_second == "done")){
console.log("Both done loading, execute more code here");
}
</script>
【问题讨论】:
标签: javascript html jquery ajax timeout