score:0
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);
}
Source: stackoverflow.com
Related Query
- keep calling a function until a criteria is hit
- React - Button Pressed, keep calling function
- How to keep Reactjs API calling function in independent page and call from component?
- React Functional component: calling as function vs. as component
- Calling Dispatch function from a blank javascript file
- Test React component method is calling function pass as a prop
- Is there a way to both destructure a function parameter, and keep a named reference to the parameter?
- Anchor tag isn't calling the onClick function in React
- Calling a function in React
- Keep the current page rendered until the new page is loaded with React.lazy and React.Suspense
- Calling functions inside a map() function in React render()
- React: Wait until all childs are avaiable and call a function once afterwards
- Calling async function inside non-async function in React-Native - Firebase
- Uncaught TypeError when calling drawImage function
- react calling a function in a child component
- Calling one async function inside another in redux-thunk
- Traversing children's children and adding function to all inputs while keep other children untouched
- In React, why do I have to bind an onClick function rather then calling it?
- Calling function after Animate.spring has finished
- Setting the default value inside the input field in the antd library after calling the function in useEffect
- Mocking a function jest but jest calling original function
- mapDispatchToProps and directly calling dispatch function in component
- Why does calling the set function of a useState hook apply immediately in async functions?
- Does calling curried function in render have negative performance impact?
- In React why does calling a function from another function in our code not work and display?
- React: Calling a function inside a map function
- Calling a function in React: Uncaught TypeError: ...is not a function(…)
- Error: sending a transaction requires a signer while calling aggregate function of Multicall Contract Ethereum
- Calling a function recursively in the context of "export default"?
- Using onClick function on a styled-components button doesn't change state until the second click (added sandbox URL)
More Query from same tag
- Adjusting toolbar height for material-table
- Avoid rendrawing chart, D3
- useSelector hooks problem after submitting data
- How to run useEffect (and a cloud firestore query) only when object is defined?
- can't resolve firebase/app error in google authentication in reactjs
- In React, what is the difference these useEffect?
- How to pass Array param in axios get to spring controller?
- Using Promise when extracting data from Supabase into React
- Very simple state change of specific array item in React
- Why does useReducer first parameter get set to Value of dispatch?
- Redux React: Not able to access 'this' inside a function of another class
- React query shows previous page data in new page
- React function not displaying in browser
- TypeError: Cannot read property 'map' of undefined Reactjs
- Strange using React stateless components
- React Native Promise, setState, and Navigation Issue
- React, how to do an include on an array within an axios requested object
- extend style material UI
- Do not understand Uncaught Typerror Cannot Read Property of 0 Undefinded?
- how to test the below given function using jest?
- i18next not detecting language variation, eg: en-us
- React - how to create a dynamic (using router) details pages
- How Do I Pass State (Value) when Calling Component in React?
- Confused on blob:url and converting it to base64 in react-dropzone
- Access callback in React
- React JS mapping / displaying elements
- Why does this generate "Undefined" when I tell it to generate a message if it doesn't exist?
- Get updated state after redux action
- How to upgrade version of Nodejs in windows
- react-router only goes to index route