Home >Web Front-end >JS Tutorial >Detailed explanation of js immediate execution function instance
This article mainly shares with you js immediate execution function examples, hoping to help everyone.
for(var i=0;i<5;i++){ (function(i){ setTimeout(function(){ console.log(i); },0); })(i); } //0 1 2 3 4
The immediate execution function will execute the code inside immediately. Even if it contains asynchronous execution code, it will be executed immediately.
for(var i=0;i<5;i++){ setTimeout(function(){ console.log(i); },0); } //5 5 5 5 5
Related recommendations:
Example detailed explanation of JavaScript immediately Execution function
Detailed explanation of JavaScript anonymous, named function and immediate execution function IIFE
Immediate execution function in JS
The above is the detailed content of Detailed explanation of js immediate execution function instance. For more information, please follow other related articles on the PHP Chinese website!