我有代码:
function images(){
$.get("page").done(function(data) {
manipulation to get required data
});
}
我试图从 get 函数内部获取一个变量,使用全局变量、返回值和函数外部的函数。有谁知道我如何能够从 get 函数内部获取一个变量并在我调用时将其作为值返回images()
?
我有代码:
function images(){
$.get("page").done(function(data) {
manipulation to get required data
});
}
我试图从 get 函数内部获取一个变量,使用全局变量、返回值和函数外部的函数。有谁知道我如何能够从 get 函数内部获取一个变量并在我调用时将其作为值返回images()
?
你不能这样做,因为它是$.get()
异步执行的。解决方案是在图像中使用回调来处理 $.get() 返回的值
function images(callback){
$.get("page").done(function(data) {
manipulation to get required data
var d = ? //manipulated data that was supposed to be returned
callback(d);
});
}
然后
//calls images
images(function(data){
//this method will get called when the $.get() is completed, and data will be the value passed to callback(?) in the done function
})
更多:阅读此