我有代码:
function images(){
$.get("page").done(function(data) {
manipulation to get required data
});
}
Run Code Online (Sandbox Code Playgroud)
我试图从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);
});
}
Run Code Online (Sandbox Code Playgroud)
然后
//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
})
Run Code Online (Sandbox Code Playgroud)
更多:阅读本文