1

私はコードを持っています:

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

グローバル変数、戻り値、関数外の関数を使用して、get 関数内から変数を取得しようとしました。get 関数内から変数を取得し、呼び出した場合に値として返す方法を知っている人はいますimages()か?

4

1 に答える 1

9

$.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
})

詳細:これを読む

于 2013-11-02T09:45:40.903 に答える