简体   繁体   English

Javascript-循环内循环

[英]Javascript - loop inside a loop

I have the following code: 我有以下代码:

 for (i = 0; i < 3; i++){ //big loop
    console.log("Start of round" + i)
    for (s = 0; s < 5000; s++){ //small loop aka delay loop
        console.log("End of round" + i);
    }
    s = 0; //reset

}

Just to begin with - I know that there are other ways to solve this. 刚开始-我知道还有其他方法可以解决此问题。 Thing is, I am trying to learn further about loops so I picked this example, and please tell me where I am wrong. 问题是,我试图进一步了解循环,所以我选择了此示例,请告诉我哪里错了。

so I have a loop that is supposed to run itself 3 times. 所以我有一个循环,应该运行3次。 and each time it runs itself, there is a loop that is supposed to delay the proceeding. 并且每次它自己运行时,都会有一个循环来延迟该过程。 Problem is, the delay stops happening at the 2nd and 3rd times. 问题是,延迟在第二次和第三次停止发生。

Here is what I think that is supposed to happen 我认为这是应该发生的

1) first i = 0, proceed with loop 1)首先i = 0,继续循环

2)Delay further proceeding with a loop inside 2)进一步延迟内部循环

3)when delaying loop is over, reset the s var of the small loop, so it would run itself again when i = 1; 3)延迟循环结束后,重置小循环的s var,以便在i = 1时再次运行。 and the bigger loop will start again 然后更大的循环将再次开始

4) big loop starts again as i = 1; 4)当i = 1时,大循环再次开始; so proceed, run the delaying loop once again, because we have reset var s in the last time. 因此,请再次运行延迟循环,因为上次我们已重置var。

5) repeat when i = 2 5)当i = 2时重复

What is it that I am missing here? 我在这里想念什么? I would like a deeper knowledge about javascript loops. 我想更深入地了解javascript循环。 thank you. 谢谢。

There is no real difference in delay, all your seeing is the console struggling to update really fast in a for loop. 延迟没有真正的区别,您所看到的只是控制台在for循环中难以快速更新。

 for (i = 0; i < 3; i++){ //big loop console.log("Start of round" + i); console.time(i); for (s = 0; s < 5000; s++){ //small loop aka delay loop console.log("End of round" + i); } console.timeEnd(i); } 

When you log the processing time, you will see the numbers are pretty much the same. 当您记录处理时间时,您会看到数字几乎相同。

If you remove the log line, you can see it more clearly the loops are the same +- a millisecond 如果删除日志行,则可以更清楚地看到它,循环是相同的+-毫秒

 for (i = 0; i < 3; i++){ //big loop console.log("Start of round" + i); console.time(i); for (s = 0; s < 5000; s++){ //small loop aka delay loop } console.timeEnd(i); } 

And the browsers will optimize code since you want faster code as a developer and user. 浏览器将优化代码,因为您希望以开发人员和用户的身份获得更快的代码。

If you really want a delay you need to use setTimeout or setInteral 如果您确实需要延迟,则需要使用setTimeout或setInteral

 var count = 0; function round () { console.log(count); count++; if(count<3) { window.setTimeout(round, 2000); } } round(); 

First of all you don't need to use a loop as a delay instead you can use: 首先,您不需要使用循环作为延迟,而可以使用:

setTimeout(functionReference, timeOutMillis);

Second: You don't need to reset the s. 第二:您不需要重置。 When you enter the second loop it will be automatically set to 0. 当您进入第二个循环时,它将自动设置为0。

I supposed that you did this because in JavaScript, threads doesn't exists, your code actually works on my browser, tw you don't need that s=0; 我以为您是这样做的,因为在JavaScript中,线程不存在,您的代码实际上在我的浏览器上有效,因此您不需要s=0; since the for statement do it for you. 因为for语句为您完成。

for (i = 0; i < 3; i++){ //big loop
    console.log("Start of round" + i)
    for (s = 0; s < 5000; s++){ //small loop aka delay loop
        console.log("End of round" + i);
    }

}

Instead of using that delay you can use the setTimeout function: 可以使用setTimeout函数来代替使用该延迟:

setTimeout(function, timeInMilliseconds);

And put your logs into a function and run it, this is a way to use 'Threads' without being 'Threads'(since doesn't work the same as one) 并将您的日志放入函数中并运行它,这是一种使用“线程”而不使用“线程”的方法(因为它的工作原理与以前不同)

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

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