Home  >  Article  >  Web Front-end  >  Detailed explanation of js immediate execution function instance

Detailed explanation of js immediate execution function instance

小云云
小云云Original
2018-03-08 15:20:301534browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn