Javascript回调 - 如何返回结果? [英] Javascript callback - how to return the result?

查看:151
本文介绍了Javascript回调 - 如何返回结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在JS中我调用一个函数,然后使用dojo调用一个PHP函数rpc Json服务。我已经走过firebug的函数,PHP正在执行并通过回调返回正确的响应,但我不知道如何返回值到调用JS函数的初始JS变量?例如

  JS函数1 

函数one(){

Var test = getPhp(number);

}

函数getPhp(number)
{

this.serviceBroker = new dojo.rpc.JsonService(baseUrl +'/ index / json-rpc /');

var result = serviceBroker.phpFunc(number);

result.addCallback(
function(response)
{
if(response.result =='success')
{
return response.description;
//我试图将此值传回
//函数一中的var测试值

}
}
);
}

基本上我现在需要传递response.description回我的var测试变量功能一。



任何帮助是值得赞赏的

解决方案

回调异步运行。这意味着getPhp函数在执行回调之前返回(这是回调的定义,异步编程的一个原因很难;-))。



你想做的是创建一个使用 test 变量的新方法。您需要在执行回调时调用此方法。



  function one(result){
var test = result;
//你喜欢的任何事情
}

函数getPhp(number,callback){
this.serviceBroker = new dojo.rpc.JsonService(baseUrl +'/ index / json-rpc /');
result.addCallback(
function(response)
{
if(response.result =='success')
{
callback(response.description );
}
}
);
}

getPhp(number,function(result){one(result);});

这最后一个方法创建一个匿名函数,传递给getPhp函数。此函数在响应到达时执行。这样,您可以在数据到达后之后将数据传递给一个(数字)函数


I am struggling to totally understand callbacks and i am stumbling at the final hurdle.

Within JS I am calling a function which then calls a PHP function using a dojo rpc Json Service. I have stepped through the function in firebug and the PHP is executing and returning me the correct response via the callback but I don’t know how to return the value to the initial JS variable that invoked the JS function? E.g.

JS Function 1

Function one(){

Var test = getPhp(number);

}

function getPhp(number)
{

this.serviceBroker = new dojo.rpc.JsonService(baseUrl + '/index/json-rpc/');

    var result = serviceBroker.phpFunc(number);

    result.addCallback(
        function (response)
        {
            if (response.result == 'success')
            {
                return response.description;
               //I am trying to pass this value back to the 
               //var test value in   function one

            }
        }
    );
}

Basically i now need to pass response.description back to my var test variable in function one.

Any help is appreciated

解决方案

This is not possible, since the callback is run asynchronously. This means that the getPhp function returns before the callback is executed (this is the definition of a callback, and one of the reasons asynchronous programming is hard ;-) ).

What you want to do is create a new method that uses the test variable. You need to call this method when the callback is executed.

i.e.

function one(result) {
  var test = result;
  // Do anything you like
}

function getPhp(number, callback) {
  this.serviceBroker = new dojo.rpc.JsonService(baseUrl + '/index/json-rpc/');
  result.addCallback(
    function (response)
    {
        if (response.result == 'success')
        {
           callback(response.description);
        }
    }
  );
}

getPhp(number, function(result) { one(result); });

This last method creates an 'anonymous function' that is passed to the getPhp function. This function gets executed at the time the response arrives. This way you can pass data to the one(number) function after the data arrives.

这篇关于Javascript回调 - 如何返回结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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