jQuery - for 循环超时
我编写了一些 jQuery 来循环遍历数组中的项目并显示一个随机数,然后继续显示另一个数字。问题是我想在循环内添加一个延迟,以便它显示数字 2 秒,然后移动到下一个。所以你会看到一个随机数,它会保留 2 秒,然后继续下一个。我尝试在循环内设置超时,但没有成功。任何帮助都会非常感谢。
function RandomNumberGenerator(){
var myArray = new Array(99);
for (var i=0; i< 99; i++) {
myArray[i] = Math.floor(Math.random()*100)
myArrayTwo[i] = Math.floor(Math.random()*100)
}
for (var i=0; i< 9; i++) {
$('li.num').text(function(index) {
// timeout needs to be here somewhere
return (myArray[i]);
})
}
});
}
I have written a bit of jQuery to loop through items in a array an display a random number and then move on to display another number. The problem is I want to put a delay inside the loop so that it shows the number for 2 seconds and then moves of to the next. so you see a random number it holds for 2 seconds and then moves on to the next. I have tried putting a timeout inside the loop but that didn't work. Any help would be amazing thanks.
function RandomNumberGenerator(){
var myArray = new Array(99);
for (var i=0; i< 99; i++) {
myArray[i] = Math.floor(Math.random()*100)
myArrayTwo[i] = Math.floor(Math.random()*100)
}
for (var i=0; i< 9; i++) {
$('li.num').text(function(index) {
// timeout needs to be here somewhere
return (myArray[i]);
})
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请注意,这不是最干净/更好的解决方案,但由于没有人发布您所要求的内容,因此这里是 for 循环中延迟的实现:
note that this isn't the cleanest/better solution, but since nobody posted what you asked for, here's an implementation of the delay in your for loop :
首先,您需要将完成工作的代码(显示随机数并决定是否应该继续)放入其自己的函数中。该函数也可以是
RandomNumberGenerator
中的局部变量:上面的函数将一个数字放入
li.num
中,递增i
,并告诉浏览器在两秒内再次调用该函数(使用setTimeout
) -- 但前提是我们显示的少于100 个号码。因此,当第一次调用此函数 (
displayNumber
) 时,它会显示一个数字,并设置为在 2 秒后再次调用自身(显示另一个数字,依此类推)。如果已显示 100 个号码,则不会再次设置呼叫,因此此时重复会停止。所以你现在可以这样做:
First of all you need to put the code that does the work (displays the random number and decides if it should continue) inside its own function. This function can also be a local variable inside
RandomNumberGenerator
:The function above puts a number in
li.num
, incrementsi
, and tells the browser to call the function again (usingsetTimeout
) in two seconds -- but only if we have shown less than 100 numbers.So when this function (
displayNumber
) is called for the first time, it displays one number and sets things up to call itself again after 2 seconds (to display another number, and so on). If it has displayed 100 numbers already it does not set the call up again, so at that point the repetition stops.So you can now do:
您实际上并不希望超时“在循环中”,您希望在超时结束时更新数字,然后启动另一个超时来更新数字,然后重复,直到您的满足结束条件。
You don't really want the timeout "in the loop", you want to update the number at the end of the timeout, then start another timeout that'll update the number, and repeat, until your end condition is met.
延迟函数中的代码不起作用。只要您的代码正在运行,浏览器就不会显示任何更新,因此您必须在更改显示之前退出该函数。
因此,您必须将工作分成可以使用
setTimeout
或setInterval
调用的部分。像这样的东西:
Delaying the code in the function doesn't work. The browser won't show any updates as long as your code is running, so you have to exit out of the function before the change shows up.
So, you have to divide the work into pieces that you can call using
setTimeout
orsetInterval
.Something like this:
这是我为此使用的相当简洁的构造类型(使用
setTimeout
而不是setInterval
)。This is the fairly concise type of construct I would use for this (using
setTimeout
instead ofsetInterval
).