Why wont my loop work?

56 views Asked by At

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. Can you guys help me figure out how to loop myfunction? Thanks

1

There are 1 answers

0
Arun P Johny On BEST ANSWER

You can use a interval based solution instead of using a while loop like

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;
    }
}