In order to optimize my loop, I have devised a strategy where a function is created to determine the correct treatment. This function then assigns the appropriate value to a variable that can be used within the loop.
The approach can be summarized as follows:
export class myClass{
ref;
Treatment1(){code}
Treatment2(){code}
Treatment3(){code}
selectTreatment(){
if(condition1){
ref = Treatment1()
else if(condition2){
ref = Treatment2()
else (condition3){
ref = Treatment3()
}
executeTreatment(){
setInterval(ref(),300)
}
}
I suspect there may be an issue with the this
keyword!
To address this concern, I came up with a revised solution:
export class myClass{
ref:any;
Treatment1(){code}
Treatment2(){code}
Treatment3(){code}
selectTreatment():Function{
if(condition1){
return ()=>{Treatment1()};
else if(condition2){
return ()=>{Treatment1()};
else (condition3){
return ()=>{Treatment1()};
}
executeTreatment(){
this.ref=this.selectTreatement();
setInterval(ref(),300);
}
}