带有延迟代码修复的javascript循环
如何根据修复此代码带有延迟代码修复的javascript循环,javascript,Javascript,如何根据修复此代码 const spkz = responsiveVoice.speak; let azj = ['hi', 'hello', 'how are ya']; var i = 1; // set your counter to 1 function myLoop() { // create a loop function azj.forEach((item, index) => { setTimeout(() => { // call a 3s s
const spkz = responsiveVoice.speak;
let azj = ['hi', 'hello', 'how are ya'];
var i = 1; // set your counter to 1
function myLoop() { // create a loop function
azj.forEach((item, index) => {
setTimeout(() => { // call a 3s setTimeout when the loop is called
alert(item); // your code here
i++; // increment the counter
if (i < index) { // if the counter < 10, call the loop function
myLoop(); // .. again which will trigger another
} // .. setTimeout()
}, 10000)
})
}
myLoop();
const spkz = responsiveVoice.speak;
let azj = ['hi', 'hello', 'how are ya'];
var i = 1; // set your counter to 1
function myLoop() { // create a loop function
azj.forEach((item, index) => {
setTimeout(() => { // call a 3s setTimeout when the loop is called
alert(item); // your code here
i++; // increment the counter
if (i < index) { // if the counter < 10, call the loop function
myLoop(); // .. again which will trigger another
} // .. setTimeout()
}, 10000)
})
}
myLoop();
它仍然可以毫不延迟地执行
const spkz = responsiveVoice.speak;
let azj = ['hi', 'hello', 'how are ya'];
var i = 1; // set your counter to 1
function myLoop() { // create a loop function
azj.forEach((item, index) => {
setTimeout(() => { // call a 3s setTimeout when the loop is called
alert(item); // your code here
i++; // increment the counter
if (i < index) { // if the counter < 10, call the loop function
myLoop(); // .. again which will trigger another
} // .. setTimeout()
}, 10000)
})
}
myLoop();
const spkz=responsiveVoice.speak;
让azj=['hi'、'hello'、'你好吗'];
变量i=1;//将计数器设置为1
函数myLoop(){//创建一个循环函数
azj.forEach((项目,索引)=>{
setTimeout(()=>{//调用循环时调用3s setTimeout
警报(项);//此处显示您的代码
i++;//递增计数器
如果(i
在循环函数中不必使用forEach
。相反,您可以使用azj[i]
访问数组项。您不能像那样“暂停”javascript。settimeout
是异步的,这意味着它不会阻止同步代码的运行,因此当您运行任何类型的“for”循环时,它将一次调用所有的settimeout
const spkz = responsiveVoice.speak;
let azj = ['hi', 'hello', 'how are ya'];
var i = 1; // set your counter to 1
function myLoop() { // create a loop function
azj.forEach((item, index) => {
setTimeout(() => { // call a 3s setTimeout when the loop is called
alert(item); // your code here
i++; // increment the counter
if (i < index) { // if the counter < 10, call the loop function
myLoop(); // .. again which will trigger another
} // .. setTimeout()
}, 10000)
})
}
myLoop();
您可以创建这样的手动循环,并使用递归延迟它:
const spkz = responsiveVoice.speak;
let azj = ['hi', 'hello', 'how are ya'];
var i = 1; // set your counter to 1
function myLoop() { // create a loop function
azj.forEach((item, index) => {
setTimeout(() => { // call a 3s setTimeout when the loop is called
alert(item); // your code here
i++; // increment the counter
if (i < index) { // if the counter < 10, call the loop function
myLoop(); // .. again which will trigger another
} // .. setTimeout()
}, 10000)
})
}
myLoop();
让azj=['hi'、'hello'、'你好吗'];
var i=0;
函数myLoop(){
setTimeout(函数(){
console.log(azj[i])
我++
如果(i myLoop()代码>使用setTimeout()
已经在使用setTimeout(),一旦循环执行,就不会延迟它。=>每一次迭代都会立即启动一个setTimeout,所以,你不能像这一次所说的那样暂停javascript@是正方形,但在接受的答案中没有循环。谢谢,这一个似乎是work@Isquare因为它与你在问题中提到的答案完全相同…@Andreas抱歉!我应该链接到答案。我不知道链接问题中的答案与此相同。我将标记它复制/删除qn。。我应该仔细看看。@Isquare不要删除这个问题。把它标为复制品,这就是它的用途。