如何让Java脚本的setTimeout在函数返回值 [英] how to make Javascript setTimeout returns value in a function

查看:407
本文介绍了如何让Java脚本的setTimeout在函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用一些服务,并返回响应的功能。如果响应为FALSE,它会等待1秒再次(这可能随后返回TRUE)。

询问服务

我该怎么做才能调用我的函数checkService()一次,并获得真正的价值? (第一或第二次尝试,由函数决定)我设定的功能内的RET值,但功能总是返回第一RET,因为setTimeout的是异步的。

在换句话说,我需要一些休眠的把戏,或任何解决方案(可能是jQuery的太)。

 函数checkService(){  //这可能返回TRUE或FALSE
  变种RET = someServiceResponse();  //此处等待1秒,然后再询问服务
  如果(RET ==真){
    返回true;
  }其他{    的setTimeout(
        功能(){
            //它必须返回该服务的第二个响应
            RET = someServiceResponse();
        },
        1000
    );    //我想checkService()返回德超时后的反应
    返回RET;
  }
}功能alertResponse(){
    警报(checkService());
}


解决方案

您应该当你期望从服务结果使用一个回调函数。

这样的:

 函数checkService(回调){    //这可能返回TRUE或FALSE
    变种RET = someServiceResponse();    //此处等待1秒,然后再询问服务
    如果(RET ==真){
        回调(RET);
    }其他{        的setTimeout(
                功能(){
                    //它必须返回该服务的第二个响应
                    RET = someServiceResponse();
                    回调(RET);
                },
                1000
        );        //我想checkService()返回德超时后的反应
        返回RET;
    }
}

所以,当你要拨打的服务,你只需要做:

  checkService(功能(状态){
    警报(状态);    //这里是一些web服务响应之后,code
});

I have a function that calls some service and returns the response. If the response is FALSE, it waits 1 second to ask the service again (which then probably returns TRUE).

How can I do to call my function "checkService()" once, and get the real value? (the first or second try, decided by the function) I set the RET value inside the function, but the functions always return the first RET, because the setTimeout is asynchronous.

In other words, I need some "sleep" trick, or any solution (may be jQuery too).

function checkService() {

  //this may return TRUE or FALSE
  var RET = someServiceResponse(); 

  // here waits 1 second, then ask the service again
  if( RET == true ) {
    return true;
  } else {

    setTimeout(
        function() {
            //it must return the second response of the service
            RET = someServiceResponse();  
        }, 
        1000
    );

    // I want the checkService() return the response after de timeout
    return RET;  
  }
}

function alertResponse() {
    alert( checkService() );
}

解决方案

You should use a callback function when you expect a result from the service.

Like this :

function checkService(callback) {

    //this may return TRUE or FALSE
    var RET = someServiceResponse();

    // here waits 1 second, then ask the service again
    if( RET == true ) {
        callback(RET);
    } else {

        setTimeout(
                function() {
                    //it must return the second response of the service
                    RET = someServiceResponse();
                    callback(RET);
                },
                1000
        );

        // I want the checkService() return the response after de timeout
        return RET;
    }
}

So when you want to call the service, you just need to do :

checkService(function(status){
    alert(status);

    // Here some code after the webservice response
});

这篇关于如何让Java脚本的setTimeout在函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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