A brief analysis of the solution to the variable changes caused by javascript asynchronous execution of functions
for(var i=0;i<3;i++){ setTimeout(function(){ console.log(i) },0);} console output: 333This is because when the method is executed, the for loop has been executed and each time it is executed, it is 3 instead of 1-2-3. At this time, we can use the immediate execution function to create a copy of the variable for each loop for timer to call to solve this problem.
for (var i = 0; i < 3; i++) { setTimeout( (function () { var _i = i; return function () { console.log(_i) }; })(), 0); }Console output: 123The above brief analysis of the problem of solving variable changes caused by asynchronous execution of JavaScript functions is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.