[英]How to do asyncrhonous jQuery.get() before $(window).load()?
我希望在触发$(window).load()之前将SVG文件加载到变量中。
我使用jQuery.get()加载文件。 问题在于,该函数异步工作,并且到了读取SVG文件时,已经调用了$(window).load()。
所以我有以下代码:
var data;
$(document).ready(function () {
jQuery.get(
"my.svg",
function (_data) {
data = _data;
},
'text');
});
$(window).load( function () {
alert(data);
});
警报将显示“未定义”。 如果稍后(例如5秒后)调用它,它将显示SVG文件的内容。
有什么建议么? 谢谢!
我同意setInterval可能是您想要的解决方案,如果您想用困难的方式做一些事情,因为您永远无法确定AJAX请求将花费多长时间。
我建议重组您的代码,使其更像这样:
<script>
$(document).ready(function () {
//this can remain async: TRUE
jQuery.get(
"my.svg",
function (_data) {
//call function to do something to svg file
//if you are relying on the SVG to be there in order to call an action
//then why not wait for the SVG to load first and then call the action
svgAction(_data);
},
'text');
function svgAction(img_code){
//do something with img code now
}
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.