简体   繁体   English

嵌套循环(2级)JavaScript超时

[英]Nested loop (2 levels) Javascript timeout

There were questions with looping in Javascript and I followed them to create my loop. 在Javascript中存在关于循环的问题,我跟随他们来创建我的循环。 But my issue is that I want a double loop. 但是我的问题是我想要一个双循环。 I am having an html with a div-container that has an image. 我有一个带有图像的div容器的html。

<div id="myDiv">
<img alt="Client Logo" src="1/1-1.png" />
</div>

Images are stored in folders 1 to 7 (their names). 图像存储在文件夹1至7(其名称)中。 Hence, source of images looks like this 1/1-1.png, 3/3-264.png, 4/4-489.png etc. Files in every folder before the dash sign have the number of the folder as well. 因此,图像源看起来像这样的1 / 1-1.png,3 / 3-264.png,4 / 4-489.png等。破折号前每个文件夹中的文件也具有该文件夹的编号。 The goal: I want to show all the pictures with a timeout starting with 1/1-1.png, then 1/1-2.png, 1/1-3.png...2/1-1.png,2/1-2.png etc. one after another till 7/7-608.png. 目标:我要显示所有图片,并以1 / 1-1.png,然后1 / 1-2.png,1 // 1-3.png ... 2 / 1-1.png开始的超时, 2 / 1-2.png等。直到7 / 7-608.png。 Ie there are 608 images in every folder. 即每个文件夹中有608张图像。 I am doing this: 我正在这样做:

for (var f=1; f<=7;f++) {
for (var s=1; s<=608; s++) {
doSetTimeout(s,m);
} }
function doSetTimeout(i,f) {
var timer = i+(f-1)*608;
setTimeout(function(){$('#myDiv img').attr('src',f+'/'+f+'-'+i+'.png')},500*timer);
}

But it doesn't work. 但这是行不通的。 It used to work fine without the outer loop of f. 它过去工作正常,没有f的外部循环。 So, obviously, I am doing something wrong. 所以,显然,我在做错事。 Maybe, because the variables are global, but I don't quite get what it means in this context. 也许是因为变量是全局变量,但在这种情况下我不太明白它的含义。 Would really appreciate your help. 非常感谢您的帮助。

You have a typo. 你有错字 You are sending in (s, m) instead of (s, f) . 您正在发送(s, m)而不是(s, f)

try 尝试

for (var f=1; f<=7;f++) {
  for (var s=1; s<=608; s++) {
    doSetTimeout(s,f);
} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM