Home > front end >  This code is why every 1 s output at a time, instead of separately between 1, 2, 3, 4, 5 s output at
This code is why every 1 s output at a time, instead of separately between 1, 2, 3, 4, 5 s output at

Time:10-09

For (the let I=1; I<=5; I++) {setTimeout (function timer () {the console. The log (I); }, I * 1000);
}

CodePudding user response:

Blogger, this question is very interesting I tried find scope might be the problem? Personal understanding said the wrong words, please show,
Because of problems first timer no wait so you cycle is a moment of the five cycle out of the results, when you after the time to put an end to the cycle begins to trigger the countdown timer, the loop five times so you can understand even if end of the cycle for a defined the five timer can carry out five times, he also asked anyway blogger is not a separate trigger I wonder when the cycle has ended his already that I can't find so he simply calculated according to you give 1 * 1000, as to the thing that print is used before I yes but cycle of I, no matter how much you time interval is he finally is 1 * time,

CodePudding user response:

The building Lord: that's a good editor, and run a debug,,,

CodePudding user response:

refer to the second floor horizon_zpy response:
the building good ah, the editors and debugging, operation, and
an app JSBox

CodePudding user response:

Program runs fine, is a created five timer, the broken for 1 s, the longest for 5 s.