JS:函数执行顺序

版权声明:本文为博主原创文章,未经博主允许不得转载。你想转载请附加连接哦 https://blog.csdn.net/dmw412724/article/details/84100018
			for(var i = 0; i < 5; i++) {
				setTimeout(function() {
					console.log(i);
				}, 1000);
			}

			console.log(i + "====");

这个打印结果不是 0 1 2 3 4 5==== 

而是 5====  55555

很TMD的奇怪,不仅都是5 而且后面几个5没有时间间隔

那么所见即所得,该函数执行的顺序应该是这样

i = 0
setTimeOut1000_0
i++

i = 1
setTimeOut1000_1
i++

i = 2
setTimeOut1000_2
i++

i = 3
setTimeOut1000_3
i++

i = 4
setTimeOut1000_4
i++

i = 5

console.log(5====);

...
...
1000毫秒后

function_0(){console(5)}
function_1(){console(5)}
function_2(){console(5)}
function_3(){console(5)}
function_4(){console(5)}

猜你喜欢

转载自blog.csdn.net/dmw412724/article/details/84100018