记一IE6,7,8中出现的bug
最近项目中写了段代码,做的事情依赖于某些异步加载的组件,由于异步的操作比较多,于是采用了一种偷懒的做法。。用轮询去检测某一组件是否加载完,但在同时检测两个组件时IE8-下出问题了。额。。还是来段事例代码吧:
<p id="testp"></p>
<script>
var i = 0;
(function fn() {
document.getElementById('testp').innerHTML += 'from 1st fn(i == ' + i + ')<br />';
if (i++ < 5) {
setTimeout(fn, 100);
}
})();
(function fn() {
document.getElementById('testp').innerHTML += 'from 2nd fn(i == ' + i + ')<br />';
if (i++ < 5) {
setTimeout(fn, 100);
}
})();
</script>
我期望的输出是:
from 1st fn(i == 0)
from 2nd fn(i == 1)
from 1st fn(i == 2)
from 2nd fn(i == 3)
from 1st fn(i == 4)
from 2nd fn(i == 5)
from 1st fn(i == 6)
我自测时IE9, FF, CHROME 也确实是这样的。可IE8下却不同:
from 1st fn(i == 0)
from 2nd fn(i == 1)
from 2nd fn(i == 2)
from 2nd fn(i == 3)
from 2nd fn(i == 4)
from 2nd fn(i == 5)
from 2nd fn(i == 6)
后面的fn把前面的覆盖了!可后来仔细想想这两个fn貌似是在同一作用域下的,把前者覆盖也合情合理。呵呵,还是取两个不一样的函数名吧,避免这种有可能产生歧义的写法。