var funcs = [];
// let's create 3 functions
for (var i = 0; i < 3; i++) {
// and store them in funcs
funcs[i] = function() {
// each should log its value.
console.log("My value: " + i);
};
}
for (var j = 0; j < 3; j++) {
// and now let's run each one to see
funcs[j]();
}Run Code Online (Sandbox Code Playgroud)
它输出这个:
我的价值:3
我的价值:3
我的价值:3
而我希望它输出:
我的价值:0
我的价值:1
我的价值:2
使用事件侦听器导致运行函数的延迟时,会出现同样的问题:
var buttons = document.getElementsByTagName("button");
// let's create 3 …Run Code Online (Sandbox Code Playgroud)简单的说...
为什么
setTimeout('playNote('+currentaudio.id+', '+noteTime+')', delay);
Run Code Online (Sandbox Code Playgroud)
完美地工作,在指定的延迟后调用函数,但是
setTimeout(playNote(currentaudio.id,noteTime), delay);
Run Code Online (Sandbox Code Playgroud)
同时调用函数playNote?
(这些setTimeout()s在for循环中)
或者,如果我的解释太难阅读,这两个函数之间有什么区别?
我想要一个字符串出现字符转换为以下代码:
function initText()
{
var textScroller = document.getElementById('textScroller');
var text = 'Hello how are you?';
for(c = 0; c < text.length; c++)
{
setTimeout('textScroller.innerHTML += text[c]', 1000);
}
}
window.onload = initText;
Run Code Online (Sandbox Code Playgroud)
它不起作用..我做错了什么?
我希望我的for循环不应该立即执行,而是在每次迭代后等待超时.例如:
for(var i=0; i<10; i++) {
console.log(i);
//wait for 1000
}
Run Code Online (Sandbox Code Playgroud)
我在堆栈溢出中发现了许多像这样的解决方案:
for (var i=0;i<=10;i++) {
(function(ind) {
setTimeout(function(){console.log(ind);}, 3000);
})(i);
}
Run Code Online (Sandbox Code Playgroud)
但是在所有实现中,循环最初等待3000毫秒,然后立即执行整个for循环.有没有办法在等待1000毫秒后调用每个迭代.
我有一个进度条,我在许多迭代循环中更新.
https://jsfiddle.net/k29qy0do/32/ (在单击开始按钮之前打开控制台)
var progressbar = {};
$(function () {
progressbar = {
/** initial progress */
progress: 0,
/** maximum width of progressbar */
progress_max: 0,
/** The inner element of the progressbar (filled box). */
$progress_bar: $('#progressbar'),
/** Set the progressbar */
set: function (num) {
if (this.progress_max && num) {
this.progress = num / this.progress_max * 100;
console.log('percent: ' + this.progress + '% - ' + num + '/' + this.progress_max);
this.$progress_bar.width(String(this.progress) + '%');
}
},
fn_wrap: …Run Code Online (Sandbox Code Playgroud) for( var i=0; i<20; i++)
setTimeout(function(){
console.log(">>> "+i);
}, i*100);
Run Code Online (Sandbox Code Playgroud)
因此,上面的代码输出>>> 1920次.为了保持i它的迭代值,我使用了一个闭包:
for(var i=0; i<20; i++)(function(i){
setTimeout(function(){
console.log(">>> "+i);
}, i*100);
}(i));
Run Code Online (Sandbox Code Playgroud)
有什么问题?问题是循环控制语句,continue;我可以这样做,return;但是当我需要break;代码时,当其他人试图阅读时,代码变得反直觉.
那我该怎么办?
至少我认为在这种情况下会发生这种情况:
function MyFunc() {
var people = Array({name: 'Alex', age: 25}, {name: 'Ellen', age: 43});
for (var i=0; i<people.length; i++) {
setTimeout(function() { ShowIt(people[i].name) }, 1000); // !!!
}
}
function ShowIt(name) {
alert(name);
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误Uncaught TypeError: Cannot read property 'name' of undefined,因此看起来在setTimeout侦听器函数内部people无法访问该变量.为什么以及如何解决它?
for(var i = 0; i < 100; i++){
var foo = 5;
}
Run Code Online (Sandbox Code Playgroud)
它有效...但是这很糟糕吗?
我知道我可以var foo在外面声明,但为什么在我只在循环中使用它时呢?
我刚刚遇到了一个非常奇怪的问题(尽管已解决),但我想知道为什么会首先发生:
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)
那确实成功了,但是我不知道为什么第一个代码不起作用。有人可以向我解释吗?谢谢你!
有人可以帮助我理解为什么这段代码:
for (var i =0; i < 2; i++) {
setTimeout(console.log(i), 0);
}
console.log("aaa");Run Code Online (Sandbox Code Playgroud)
将会写:
0
1
aaa
Run Code Online (Sandbox Code Playgroud)
但该代码:
会这样写:
aaa
2
2
Run Code Online (Sandbox Code Playgroud)
请注意,我理解第二节是如何进行的。工作,我不明白为什么第一个会有所不同。
谢谢!
javascript ×10
settimeout ×6
loops ×3
jquery ×2
closures ×1
console.log ×1
css ×1
for-loop ×1
progress ×1
variables ×1