1. I recently encountered a problem during development, why is the output always 5, instead of clicking on each p, the corresponding 1, 2, 3, 4, 5 will be alerted.
The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
At this time, click on any p and the pop-up will be 5
Reason: js event processor will not run during the idle time of the thread, resulting in the final run time What is output is the last value of i, that is: 5
2. Solution: Use closure to protect the value of variable i.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
The above article briefly discusses the problem of js for loop outputting i as the same value, which is all the content shared by the editor. , I hope it can give everyone a reference, and I also hope everyone will support the PHP Chinese website.
For more articles related to the problem of js for loop outputting i as the same value, please pay attention to the PHP Chinese website!