gpt4 book ai didi

jquery - jQuery.ajax() 成功执行之前调用的其他函数

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

我想通过jQuery.ajax()获取一些数据,通过成功保存这些数据然后使用它。函数的顺序是:

$.ajax({
url: this.html_url,
cache: false,
success: function(data){
//Here the data is saved via HTML5 localStorage
}
});

doSomething();//The stored data is used in this function.

问题是在 success 函数保存下载的数据之前调用了 doSomething() 。因此,数据在保存之前就已被使用。有人可以帮忙吗?

最佳答案

这是因为 ajax 调用是异步。在 ajax 调用中设置 async : false 默认为 true

<强> DOCUMENTATION

$.ajax({    
url: this.html_url,
cache: false,
async : false, // added this
success: function(data){
//Here the data is saved via HTML5 localStorage
}
});

success 中调用 doSomething 的第二种方法

success: function(data){
//Here the data is saved via HTML5 localStorage
doSomething()'
}

关于jquery - jQuery.ajax() 成功执行之前调用的其他函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8946338/

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