我尝试从.json文件(驻留在脚本本地)中提取一个JSON对象,并将数据保存为一个变量。目前,我使用的是jQuery的$.getJson()函数,它工作得很好,我只想确保在完成脚本的其余部分之前加载了JSON数据,因为一些函数依赖于它。
有什么想法吗?
发布于 2021-11-14 17:20:16
请参阅有关getJson的文档
如果你想让它运行,其他部分应该等待,你可以设置'asysn‘:false,但这不是一个好主意,因为ajax之后的代码将等待它完成。
或
// check the meaning of done, fail, always
$.getJSON( "url", function() {
}).done(function(){
// your rest of the code
});或者您可以尝试自定义trigger event
$(function(){
$.getJSON( "url", function() {
}).done(function(){
$( document ).trigger( "jsonSuccess", [ "bim", "baz" ] );
});
$( document ).on( "jsonSuccess",function(event, var1, var2){
// will execute after the 'getJSON' success
});
// other codes can be execute from here, it will not wait
});https://stackoverflow.com/questions/69964607
复制相似问题