重复功能直到完成?

时间:2015-08-03 13:04:59

标签: javascript node.js loops concurrency

我有一个带有参数的函数我希望按顺序重复运行,直到状态代码检查正确。

    function check( id ){
    var status = 2; 
    var exit = 0;
    // set status based on web request with ID param
    switch( status ){
        // based on status code, output a message and set exit 0/1
    }

    /** Here I want the Function to repeat itself with a delay without skipping until exit is 1 **/

}

我尝试使用async.whilst库,但我无法弄清楚如何保留id参数并使用回调。

我也尝试了setTimeout方法,但它淹没了控制台并崩溃了。

提前感谢您提供给我的任何帮助。

修改

这是完整代码,它是非常贫民窟,但我是这个基于事件的东西的新手

function checkStatusLoop( tradeID ){ //checks if the trade is on-going or not ( 0 - done, 1 - ongoing )
    var status = -1; // set default to active
    var exit = 0;
    manager.getOffer( tradeID, function( err, offer ) {
        if( err ) throw err;
        status = offer.state;
    });
    switch( status ){
        case 1:
            console.log('Trade #' + tradeID + ' Invalid');
            exit = 1;
            break;
        case 2:
            console.log('Trade #' + tradeID + ' Active');
            exit = 0;
            break;
        case 3:
            console.log('Trade #' + tradeID + ' Accepted!');
            exit = 1;
            break;
        case 4:
            console.log('Trade #' + tradeID + ' Countered');
            exit = 1;
            break;
        case 5:
            console.log('Trade #' + tradeID + ' Expired');
            exit = 1;
            break;
        case 6:
            console.log('Trade #' + tradeID + ' Canceled');
            exit = 1;
            break;
        case 7:
            console.log('Trade #' + tradeID + ' Declined');
            exit = 1;
            break;
        case 8:
            console.log('Trade #' + tradeID + ' InvalidItems');
            exit = 1;
            break;
        case 9:
            console.log('Trade #' + tradeID + ' EmailPending');
            exit = 0;
            break;
        case 10:
            console.log('Trade #' + tradeID + ' EmailCanceled');
            exit = 0;
            break;
        default:
            console.log('Trade #' + tradeID + ' Bad State!');
            exit = 1;
            break;
    }
    if( exit == 0 ){
        setTimeout(function(){
            checkStatusLoop( tradeID );
        }, 2000);
    } else {
        return ( TradeOfferManager.getStateName(status) );
    }
}

此库来自github:https://github.com/DoctorMcKay/node-steam-tradeoffer-manager/wiki/TradeOfferManager#getofferid-callback

哎呀,显然有一个打开('sentOfferChanged',我在这个上花了很长时间的耻辱

1 个答案:

答案 0 :(得分:2)

 function check( id ){
    var status = 2; //default of 2
    var exit = 0;

    // set status based on web request with ID param
    switch( status ){
        // based on status code, output a message and set exit 0/1
    }

    /** Here I want the Function to repeat itself with a delay without skipping until exit is 1 **/
    //but I need the browser to update, so I cannot run check immedeately.
    setTimeout(function() {
       check( id );
    }, 100)
}