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
You can use a interval based solution instead of using a while loop like