score:0

Accepted answer

you have an infinite loop that is creating new intervals (which will run indefinitely each) on each iteration. that's what's killing your app.

the simplest solution would be (https://jsfiddle.net/29l556s9/10/):

stick = () => {
  settimeout(() => {
    if(dealerstotal() < 21) {
      dealtodealer();
      stick();
    }
  }, 3000);
};

score:0

`let interval = setinterval(()=>{

//do what you want here

if(this.dealerstotal() >= 21){ clearinterval(interval) } // keep interval loop running until dealerstotal is bigger than or equal to 21 },50)//50ms`

with your current one, you are calling while loop and this loop will create a new setinterval so if you have 10 while loop, setinterval will be called 10 times which will create infinite interval which eventually exceeds processing power. so the best is to keep interval only as while loop will block. while loop is good for definite value or until something happens such as a keyboard press. but when you want a ui multiple things happening then setinterval is the better option.

score:1

the function setinterval will be executed recurrently unless you stop it with the function clearinterval.

use the settimeout instead:

stick = () => {
   while(this.dealerstotal() < 21){
      settimeout(this.dealtodealer, 3000);
   }
}

important: if this.dealerstotal() always returns a number less than 21 your code will enter in an infinite loop.

score:1

while loop can sick your perfomance, try to play around with using only interval. example:

  stick = () => {
  this.interval = setinterval(() => {
   if(this.dealerstotal() < 21) {
    this.dealerstotal();
    clearinterval(this.interval)
   }
  }, 3000);
 }

Related Query

More Query from same tag