How can variables generated by the js loop be reassigned elsewhere?

scenario: multiple variables are initialized in a loop (T1, T2, T3, etc.), and these variables need to be assigned values elsewhere. Through t+index (1, 2, 3) Way to get a variable, but the result is that the string cannot be assigned. How do I get the name of a variable?

Oct.27,2021

I have a suggestion: instead of using so many variables, we can uniformly maintain it in an array and obtain and assign values according to the subscript when accessing. Can we achieve the needs of the landlord?


variable name string can be operated as eval


look at the code of the subject, and the general idea is to use a loop to run setTimeout and setInterval . Here I point out the strange place:

timerFlagArr.push(timerFlagObj);
...
clearTimeout(timerFlagArr[idx].rId);
clearInterval(timerFlagArr[idx].oId);
...
timerFlagObj.oId = o;
timerFlagObj.rId = r;

here you obviously want to use an array to store several objects returned by setTimeout and setInterval for later destruction. But the later assignment uses timerFlagObj , which is actually the called array when it is destroyed, and there is no connection between the two.
suggest : save in an array and destroy it with an array at the same time (just because I push an object in the array doesn't mean that if I change the object later, I can change the object in the array at the same time

Menu