简体   繁体   English

为什么我的循环无法正常工作?

[英]Why wont my loop work?

I've been trying to loop a function but i cant seem to make it work as i want it to. 我一直在尝试循环一个函数,但是我似乎无法使其按我的意愿工作。 Below is the my loop 下面是我的循环

console.log('START LOOP MY FUNCTION');
a=1;
do{
    console.log('CALL MY FUNCTION');
    a=myFunction(a);
    console.log('EXIT MY FUNCTION');
}while(a==1);
console.log('EXIT LOOP MY FUNCTION');

And this is my function 这是我的职责

function myFunction(b) {
    setTimeout(function(){
        console.log('Start of My function');
        console.log('Inside b is '+b);
        var results = $('#dice > div.game > div > div.bet-history > table > tbody > tr');
        var result = $(results[0]).children('.dice-profit').children().text();
            console.log('BEFORE IF');
            if(result.substring(1) != $(betField).val()){
                console.log('################ERROR FOUND - Result:'+result.substring(1)+' Bet:'+$(betField).val()+' NOT EQUAL');
                console.log('AFTER IF');
                return b=1;
            }else{
                console.log('NO EROR YOU MAY NOW EXIT LOOP');
                console.log('AFTER ELSE');
                return b=0;
            }
        }, 3000);
    }

This is the output in console 这是控制台中的输出

new:232 START LOOP MY FUNCTION
new:235 CALL MY FUNCTION
new:237 EXIT MY FUNCTION
new:239 EXIT LOOP MY FUNCTION
new:38 Start of My function
new:39 Inside b is 1
new:42 BEFORE IF
new:48 NO EROR YOU MAY NOW EXIT LOOP
new:49 AFTER ELSE

I think it should work but from the looks of the output in the console, it already exited the loop before calling myfunction meaning it wont loop even if b=1. 我认为它应该可以工作,但是从控制台输出的外观来看,它在调用myfunction之前已经退出了循环,这意味着即使b = 1也不会循环。 Can you guys help me figure out how to loop myfunction? 你们能帮我弄清楚如何循环功能吗? Thanks 谢谢

You can use a interval based solution instead of using a while loop like 您可以使用基于间隔的解决方案,而不是使用while循环

console.log('START LOOP MY FUNCTION');
//start the function
loop(function () {
    //this callback will be called once `a` becomes 1
    console.log('EXIT LOOP MY FUNCTION');
})

function loop(callback) {
    var a = 1,
        timer;
    //to run myFunction every 3 seconds
    timer = setInterval(function () {
        console.log('CALL MY FUNCTION');
        a = myFunction(a);
        console.log('EXIT MY FUNCTION');

        //check if `a` is 1 if so terminate the calls
        if (a != 1) {
            clearInterval(timer);
            callback();
        }
    }, 3000);
}

//This function should not do any async ops, it should just check the condition and return the value
function myFunction(b) {
    console.log('Start of My function');
    console.log('Inside b is ' + b);
    var results = $('#dice > div.game > div > div.bet-history > table > tbody > tr');
    var result = $(results[0]).children('.dice-profit').children().text();
    console.log('BEFORE IF');
    if (result.substring(1) != $(betField).val()) {
        console.log('################ERROR FOUND - Result:' + result.substring(1) + ' Bet:' + $(betField).val() + ' NOT EQUAL');
        console.log('AFTER IF');
        return 1;
    } else {
        console.log('NO EROR YOU MAY NOW EXIT LOOP');
        console.log('AFTER ELSE');
        return 0;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM