无法使用 setInterval 迭代整个字母表
Can't iterate whole alphabet using setInterval
我希望代码每秒迭代每个字母,但它在一秒钟内迭代了整个字母表。
let alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
setInterval(function() {
for (let i = 0; i < alphabet.length; i++) {
console.log(alphabet[i]);
}
}, 1000);
如果你想在一秒后一个接一个地获取元素,那么你可以使用setTimeout而不是setInterval
setTimeout vs setInterval
let alphabet = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
];
for (let i = 0; i < alphabet.length; i++) {
setTimeout(function () {
console.log(alphabet[i]);
}, i * 1000);
}
不用啊循环简单的区间就够了。如果迭代到达数组末尾,使用 clearInterval
停止迭代
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
function run() {
let i = 0;
let interval = setInterval(() => {
if (i === alphabet.length) {
clearInterval(interval);
return
}
console.log(alphabet[i])
i++
}, 1000)
}
run()
您不需要在 setInterval
中包含 for 循环。相反,您只需要声明一个 index
并在 setInterval 中更新它,一旦索引超出数组范围,您需要清除 timer
.
let alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
let index= 0;
const timer = setInterval(function () {
index< alphabet.length
? console.log(alphabet[index++])
: clearInterval(timer);
}, 1000);
发生这种情况是因为您遍历了整个数组,为避免这种情况,您需要为迭代添加索引 +1。
let alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
let i = 0;
let intervalID = setInterval(function (){
console.log(alphabet[i]);
i++;
i < alphabet.length ? null : clearInterval(intervalID);
}, 1000);
这里只要setInterval就够了。无需添加循环。
let alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
let counter = 0;
let interval = setInterval( () => {
console.log( alphabet[ counter++ ] );
if( counter === alphabet.length ){
clearInterval( interval );
}
}, 1000 );
这个怎么样?从关闭开始清除间隔。
const alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
let i = 0
const timerId = setInterval(function (){
if(i === alphabet.length){
clearInterval(timerId);
console.log('Clear interval')
}else{
console.log(alphabet[i++]);
}
}, 1000);
我希望代码每秒迭代每个字母,但它在一秒钟内迭代了整个字母表。
let alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
setInterval(function() {
for (let i = 0; i < alphabet.length; i++) {
console.log(alphabet[i]);
}
}, 1000);
如果你想在一秒后一个接一个地获取元素,那么你可以使用setTimeout而不是setInterval
setTimeout vs setInterval
let alphabet = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
];
for (let i = 0; i < alphabet.length; i++) {
setTimeout(function () {
console.log(alphabet[i]);
}, i * 1000);
}
不用啊循环简单的区间就够了。如果迭代到达数组末尾,使用 clearInterval
停止迭代
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
function run() {
let i = 0;
let interval = setInterval(() => {
if (i === alphabet.length) {
clearInterval(interval);
return
}
console.log(alphabet[i])
i++
}, 1000)
}
run()
您不需要在 setInterval
中包含 for 循环。相反,您只需要声明一个 index
并在 setInterval 中更新它,一旦索引超出数组范围,您需要清除 timer
.
let alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
let index= 0;
const timer = setInterval(function () {
index< alphabet.length
? console.log(alphabet[index++])
: clearInterval(timer);
}, 1000);
发生这种情况是因为您遍历了整个数组,为避免这种情况,您需要为迭代添加索引 +1。
let alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
let i = 0;
let intervalID = setInterval(function (){
console.log(alphabet[i]);
i++;
i < alphabet.length ? null : clearInterval(intervalID);
}, 1000);
这里只要setInterval就够了。无需添加循环。
let alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
let counter = 0;
let interval = setInterval( () => {
console.log( alphabet[ counter++ ] );
if( counter === alphabet.length ){
clearInterval( interval );
}
}, 1000 );
这个怎么样?从关闭开始清除间隔。
const alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
let i = 0
const timerId = setInterval(function (){
if(i === alphabet.length){
clearInterval(timerId);
console.log('Clear interval')
}else{
console.log(alphabet[i++]);
}
}, 1000);