哪位大佬来帮帮小弟啊?

为什么在if语句不成立的情况下还能执行里面的setTimeout事件呢

<div id="light" class="white_content">
	<a href="javascript:void(0)" onclick="two()" style="z-index:99;left: -5px;">×</a>
	<a href="">
		<img  src="./img/shang.jpg" width="100%" height="100%" alt="" >
	</a>
</div>
<script>
	function one(){
	 document.getElementById('light').style.display='block';
	}
	function two(){
	 document.getElementById('light').style.display='none';
	}
	var a1 = document.getElementById('light');
	a1.style.display='none';
	var i = 0;
	while (i < 100) {
		if(a1.style.display == 'none'){
		  (function(i) {
			setTimeout(function() {
				console.log(a1.style.display);
				one();
				console.log(i)
			}, 3000 * i)
		  })(i++)
		}
	}
</script>

 

if语句不成立,哪里不成立了,你在上面不是设置了display='none'吗,然后你再在下面用display=='none'判断,肯定会成立啊。

你这个while也很迷,如果if语句不成立,i不能递增,不是造成死循环了?

按照你的逻辑,你应该把if放到setTimeout里面,在setTimeout里面去判断display,然后执行下面的逻辑。

我打印了一下if是成立的。。。。。