获取 jQuery get().done() 函数的返回值
get return value of jQuery get().done() function
我有代码:
function images(){
$.get("page").done(function(data) {
manipulation to get required data
});
}
我尝试使用全局变量、返回值和函数外部的函数从 get 函数内部获取变量。有谁知道如果我调用images()
,我将如何从 get 函数内部获取变量并将其作为值返回?
你不能这样做,因为$.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
})
更多:阅读此内容
相关文章:
- 根据是否解析了 Promise 从函数返回值
- Angular,函数在(模型)工厂中返回值
- Javascript中带有返回值的嵌套函数
- 从Ajax函数返回值
- HTML如何根据javascript函数的返回值限制文本输入
- 使用在函数外部声明的变量的 Javascript 返回值 + undefined
- 如何从嵌套的API函数返回值
- 如何记录调用另一个函数的函数的返回值
- 即使使用回调函数也无法返回值
- PHP 函数不会向 JavaScript 变量返回值
- 从异步函数返回值
- 我如何才能继续'如果'语句来比较作为参数的多个函数返回值
- 从节点中的回调函数返回值
- 从另一个文件访问函数的返回值
- 从JSP页面调用Java Script函数未返回值
- jquery csv函数,如何返回值
- Scala提升了如何运行javascript函数并将返回值存储在变量中
- 等待函数完成,然后将返回值分配给变量JS
- 来自函数 Javascript 的 NaN 返回值 ||函数执行顺序
- 在 html 输入字段中使用返回值函数