获取jQuery get()。done()函数的返回值 [英] get return value of jQuery get().done() function

查看:929
本文介绍了获取jQuery get()。done()函数的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码:

function images(){
    $.get("page").done(function(data) {
        manipulation to get required data
    });
}

我试图从get函数中获取变量,使用全局变量,返回值和函数外部的函数。有没有人知道如何从get函数内部获取变量并将其作为值返回如果我调用 images()

and I have tried to get a variable from inside the get function, using global variables, return values, and functions outside of the function. Does anyone know how I would be able to get a variable from inside the get function and return it as a value if I called images()?

推荐答案

由于 $。get()异步执行,因此无法执行此操作。解决方案是使用图像中的回调来处理$ .get()返回的值。

You can't do it since $.get() executes asynchronously. The solution is to use a callback in images to process the value returned by $.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
})

更多:阅读本文

这篇关于获取jQuery get()。done()函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆