我刚刚遇到了一个非常奇怪的问题(尽管已解决),但我想知道为什么会首先发生:
function stuffAppear() {
var i;
for (i = 0; i < speech.length; i++) {
apperance(i);
}
}
function apperance(i) {
var x = speech[i];
setTimeout(function() {$(speech[i]).fadeIn(1000); console.log(i);}, 1000 + i * 1500);
console.log(speech[i]);
}
Run Code Online (Sandbox Code Playgroud)
控制台日志显示“#yo0”,然后显示“#ma0b”(这是必需的),但与此同时,它们从未消失
我一直在玩代码,直到达到以下要求:
function stuffAppear() {
var i;
for (i = 0; i < speech.length; i++) {
apperance(i);
}
}
function apperance(i) {
var x = speech[i];
setTimeout(function() {$(x).fadeIn(1000); console.log(i);}, 1000 + i * 1500);
}
Run Code Online (Sandbox Code Playgroud)
那确实成功了,但是我不知道为什么第一个代码不起作用。有人可以向我解释吗?谢谢你!