最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - get return value of jQuery get().done() function - Stack Overflow

matteradmin4PV0评论

I have the code:

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

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()?

I have the code:

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

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()?

Share Improve this question asked Nov 2, 2013 at 9:43 Lugia101101Lugia101101 6711 gold badge7 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

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);
    });
}

then

//calls images
images(function(data){
    //this method will get called when the $.get() is pleted, and data will be the value passed to callback(?) in the done function
})

More: read this

Post a comment

comment list (0)

  1. No comments so far