Instead of seeing 1 alert message every 3 seconds, what you get is a 3-second delay followed by 10 alert messages in quick succession! ... How do I add a delay in a JavaScript loop? For example: console. Questions and posts about frontend development in general are welcome, as are all posts pertaining to JavaScript on the backend. alert(“hell88o”); // your code here Asking for help, clarification, or responding to other answers. Thank you! Better wording might be, "When the timeout actually happens and the call to. Is there a way to determine the order of items on a circuit? A chimpanzee and two trainees could run her! adding delay in javascript foreach loop . How to deal lightning damage with a tempest domain cleric? In your specific case, there is very little that's actually enclosed in the closure created by the k function. The For Loop is the most basic way to loop in your JavaScript code. The “For” Loop. For repeatedly calling some function every X milliseconds, one would normally use setInterval(). Can humans learn unique robotic hand-eye coordination? })(5); // pass the number of iterations as an argument You can specify the amount of time GoLive waits for JavaScript code to return control before it exits the current script unconditionally. log ( iterableArray [ index ]); index += 1 ; setTimeout ( delayedIteration. Which “href” value should I use for JavaScript links, “#” or “javascript:void(0)”? I loved this. }. Below given example illustrates how to add a delay to various loops: For loop: … Hi Infinite loops. How do you analyze master games without annotations? This is where we need to use a setInterval (). via GIPHY. Following is the code for adding delay in a loop −Example Live Demo Before ECMA Script 5, we had only two ways of introducing delays in JavaScript. How do I include a JavaScript file in another JavaScript file? So, what to do? let delay = 5000; let timerId = setTimeout(function request() { ...send request... if (request failed due to server overload) { // increase the interval to the next run delay *= 2; } timerId = setTimeout(request, delay); … This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. I kind of got it but it quickly turned into a mess in my head. Import image to plane not exported in GLTF. I want to delay a "for loop" for a while but don't know how to do it. } Enter your email address to be notified of new posts: Click to share on Telegram (Opens in new window), Click to share on WhatsApp (Opens in new window), Click to share on Facebook (Opens in new window), Click to share on Twitter (Opens in new window), Click to share on Reddit (Opens in new window), Click to share on Tumblr (Opens in new window), Click to share on Skype (Opens in new window), Click to share on LinkedIn (Opens in new window), Click to share on Pinterest (Opens in new window), Click to share on Pocket (Opens in new window). Normally, we do this with setTimeout (). The last line calls the function theLoop, and passes it the value 10 for i. var numChildren = parElement.children.length; With setTimeout, you execute a function. This is fairly straightforward. javascript by subodhk on Apr 26 2020 Donate . Now, most likely, in order to alert “Cheese!” ten times (with a 3-second delay between each alert), the first thing you’ll probably try is this: The above code will not do what you want, though. Required fields are marked *. My bxSlider horizontally spans 100% of the browser width - … What I mean is the call to k will be made as if you have written it in a plain tag: that is to say, outside of any other function calls. childWeekLoop(y); it didnt blink every half a second it stayed ON ,So for some reason it got stuck in the while loop . EEK!! How to fix a cramped up left hand when playing guitar? The two key methods to use with JavaScript are: setTimeout(function, milliseconds) Executes a function, after waiting a specified number of milliseconds. }, 1000); Then t is set to TIME and is corrected in case of a … childLoop(x); THEN it is called again. There is a check that says if we decrement i, and it’s still greater than 0, we should call theLoop(i) again. An infinite loop can freeze your computer, making your computer unresponsive to … Note that this if statement and the subsequent calling of theLoop is not queued instantly, because all the code inside the setTimeout will only execute after 3 seconds has elapsed. This subreddit is for anyone who wants to learn JavaScript or help others do so. I was using the function insider the loop and not vice-versa. To add delay in a loop, use the setTimeout() metod in JavaScript. If you’d like to get really fancy, you can do this: If any of this is confusing, or if you’d really like to understand more about some of the pitfalls and complexities of JavaScript, you seriously need to read K. Scott Allen’s excellent explanations of things like this, Function.apply vs. Function.call, variable scopes, and closures in JavaScript: And finally, don’t miss John Resig on How Javascript Timers Work! rev 2021.2.22.38628, Stack Overflow works best with JavaScript enabled, Where developers & technologists share private knowledge with coworkers, Programming & related technical career opportunities, Recruit tech talent & build your employer brand, Reach developers & technologists worldwide, "scope" pertains to a function's lexical context, not to the call stack. As far as I got, the answer is ” Thread.sleep ” but I have a feeling it is not what I am looking for. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. ': AP criticized for saying Covid vaccination WON'T bring life back to normal, Myanmar protesters hold general strike as military warn lethal force possible, BEST OF THE WEB: One-Third of Deaths Reported to CDC After COVID Vaccines Occurred Within 48 Hours of Vaccination, Space hurricane observed for the first time. this video (starting at 7:46) from Paul Irish, Choosing Java instead of C++ for low-latency systems, Podcast 315: How to use interference to your advantage – a quantum computing…, Opt-in alpha test for a new Stacks editor, Visual design changes to the review queues, Randomize setInterval ( How to rewrite same random after random interval), JS global variable not being set on first iteration. in a setTimeout() whose duration is greater to or equal than the for loop timeouts, we ensure The loop is done! How do I remove a property from a JavaScript object? Notify me of follow-up comments by email. Using setTimeout in the example loop will not behave as expected, if you expect that there will be a one second interval between each task. By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy. It is very handy to execute a block of code a number of times. Without this feature, an infinite loop or other failure in an extension’s JavaScript code could halt GoLive indefinitely. Could you show me the direction to the Java way of this tutorial ? For example. Execution wise, it isn't much different from doing this: setTimeout just gives the browser a chance to 'breath' if you will. That function sets up a event for the function to run again -- but here is the important difference: The function exits, completely, and is gone[1]. setInterval(function, milliseconds) Same as setTimeout(), but repeats the execution of the function continuously. setTimeout(function () { })(8); JavaScript closure inside loops – simple practical example. Thank you for this tutorial. Adding days in a date using the Field Calculator, Why do we use '$' sign in getRecord wired function. It doesn't, to use the right terminology, block the browser. Like the cat, you will also become a Master of JavaScript Looping, after you know all the looping tricks. log … But what if you want to so something 10 times, and delay 3 seconds between iterations? FreeCodeCamp | Codey McCodeface. (the stack is never deallocated). Very well written and explained. * Note: I'm not using the strict meaning of scope as defined in ECMAScript spec here. 7. Seems like that would be easy. The only significant closure is the reference to the arguments cb and myId. If you’ve ever programmed something in JavaScript, you most likely ran into a situation where you needed a delay. (function childWeekLoop (y) { Usually it appears in this form: When you want to check for something often (here every 10 milliseconds), it can be better to use this pattern instead of setInterval because it can improve your page performance. Unfortunately, I don’t have a non-proprietary example to show you, but struggling is good! Well, non-intuitive to me, anyway… Maybe this might help: https://www.sitepoint.com/demystifying-javascript-variable-scope-hoisting/. ( the stack is never deallocated). * Time delay loops have a specific function of delaying execution. JavaScript executes linearly, one line after another. I remember the first time I read about infinite loop and the fear that gripped my mind when I … Line 40 is the one that confuses me. Thanks for contributing an answer to Stack Overflow! Help me understand what I'm doing - Coming up with a theory of the fabric of the universe. length) { return ; } console. The code works like so: Well, the queuing of the setTimeout function and the for loop iterations happen “instantly”, or nearly so. It may look like recursion, but setTimeout does not create recursion. Let's say this "for loop" runs from 0 to 8 and after each i there should be a delay for 2 sek. When I introduce a delay and try to mock it with a fake timer, the generator does not yield on the second loop and the test fails. console.log(`x: ${x}, y: ${y}`); In fact, it's a pretty commonly used concept. (function childLoop (x) { The solution is not as obvious as it appears…. I was asked (by a friend) to build a timer (infinite one which writes a line every second), but without setInterval. Does the Victoria Line pass underneath Downing Street? 'Why would anyone get the jab after this? JavaScript. Viewed 15k times 11. I use it for all my projects now, and I have yet to find an issue with it in any browser. Turns out it’s not-so-straightforward, but doable. console.log(`----------`); Nope. For repeatedly calling some function every X milliseconds, one would normally use setInterval (). FreeCodeCamp | Codey McCodeface, Studio Space: Dust Sonification – Filling the Void, https://www.sitepoint.com/demystifying-javascript-variable-scope-hoisting/, https://scottiestech.info/2017/10/03/pikajs-think-jquery-but-smaller-lighter-and-just-as-easy-to-use/, How to Easily Print a Large Image to Multiple Pages in Windows, Fix the grey X on shortcut icons in Windows, Fix weird colors in Chrome in just a few clicks. We want The loop is done! function sortlastToFirst() { for (let index = 0; index 0 in the delay finction Myticks increasing evey 1microsecond is comparing myticks to us value in a while loop. So, this code works like so: That’s exactly what we want: 10 alerts, and each alert pops up 3 seconds after the previous one. Next, the setTimeout function will call the code inside after 3 seconds have passed. Method 1: Using an infinite loop to keep checking for the elapsed time The time that the sleep function starts is first found using the new Date().getTime() method. if (y==0 && --x) { @Lee: Clarified what I meant. I ignored it in my answer, however, out of simplicity. The do/while loop is a variant of the while loop. I was asked (by a friend) to build a timer (infinite one which writes a line every second), but without setInterval. First of all, we’re using a self-invoking function. PTIJ: Oscar the Grouch getting Tzara'at on his garbage can, Grep command not returning expected results for testing. It actually creates a recursion and after a while (week or something) - the process will consume much memory. 2. Create a Simple Delay Using setTimeout. Learn how your comment data is processed. Say you want an animation to run for 1 second, but then delay for 4 seconds before running again. How can I change my code in order not to be much memory consume? jQuery bxSlider Infinite Loop Delay? If you’ve ever programmed something in JavaScript, you most likely ran into a situation where you needed a delay. Pingback: Tic-Tac-Toe, Simon says…no. setTimeout(async function () { Using an infinite loop that runs till the right time is satisfied. var parElement = document.getElementById(“flex-box”); // Target every child of the element There is a huge debate of using delays in JavaScript. It does not create recursion, because the function exits completely and is then called again. Help with infinite while loop, using setTimeout. Well, that’s fine. We’re going to just use the following code as our “function”: Normally you would probably want to do something a bit more complicated than that, but we’ll just keep things simple for the sake of the example. To learn more, see our tips on writing great answers. } When you enter the loop, t0+t*60 evaluates to a value that, in this program, is guaranteed to be greater than t. Still the loop will execute 1 time, just like with FOR 5 TO 2. Note that the function expression in the OPs, @RoyiNamir—there's a closure between the anonymous function passed to. Didn't use your wording because although the wording is technically wrong, I find that more people grasp the concept more quickly this way. An infinite loop does not stop executing because the stopping condition is never reached. A chance for the screen to update, other events to process. if (--y) { (even though I had to mark the perimeter of this code with “don’t touch” comments), Now the time to pay the technical dept is coming, as I have to turn this code into Java for I am porting my mini game on smartphone ð. So far so good. Self-invoking functions are particularly tricky. Both “this” and other variables do non-intuitive things in JavaScript. This site uses Akismet to reduce spam. Thought that was happening with the que but wasn’t sure how to fix. (adsbygoogle = window.adsbygoogle || []).push({}); Awesome tutorial that made my day! The standard way of creating a delay in JavaScript is to use its setTimeout method. (function myLoop(i) { })(3); Your email address will not be published. There are some approaches that can be used to simulate a sleep function. August 30, 2014, 8:35am #1. Which equals operator (== vs ===) should be used in JavaScript comparisons? // Sorting Last to First You need to fake it. The loop will fire off all it’s iterations before the delay has finished firing once, preventing it from ever firing more than that one time. See this video (starting at 7:46) from Paul Irish for more information about this pattern. arrives behind and expires after the last for loop timeouts. 145k. Do you have any example? Pieces Of Color: When YouTube's oversensitive filters think chess videos are racist, will language have to adapt to Big Tech? I have been trying to use the “Se;f-Invoking” function inside of the loop but it doesn’t work as expected. Pingback: Studio Space: Dust Sonification – Filling the Void. One such thing is totally covered by SitePoint in their article, Delay, sleep, pause, wait etc in JavaScript. 0. Connect and share knowledge within a single location that is structured and easy to search. await createProperty(x, y); How to check whether a string contains a substring in JavaScript? setTimeout(function() { What happens instead is the whole loop is executed in under a millisecond, and each of the 5 tasks is scheduled to be placed on the synchronous JS event queue one second later. There is no other specific task of a delay loop. I haven’t programmed anything in Java in a loooong time. In this article, I want to share some gotchas to watch out for if you intend to use await in loops.. Before you begin to pass through the same process as the console.log(i) statements. The problem is that I'm afraid there's gonna be a memory pressure. I’m afraid I can’t help you there. Hi, When you initially work with loops, you may create infinite loops. Using a setTimeout timer. Join Stack Overflow to learn, share knowledge, and build your career. I tried to call delay(500000) and put the toggle before and after the loop to see if my led is blinking every half a second. BTW, if you like jQuery, check out my JS library Pika: https://scottiestech.info/2017/10/03/pikajs-think-jquery-but-smaller-lighter-and-just-as-easy-to-use/. And that’s not what you want. For instance, if your function takes more than 10 milliseconds to execute, and you use setInterval(f, 10) then it will continually be being called. JavaScript async and await in loops 1st May 2019. So the call to k ends immediately with its stack deallocated. That means that within milliseconds, all 10 of your alert(“Cheese!”); functions are queued up and ready to fire – one after the other, about 3 seconds in the future. Active 3 years, 9 months ago. This method executes a function, after waiting a specified number of milliseconds. Things get a bit more complicated when you try to use await in loops.. But it also has a stack, or a queue of operations to perform. Here is my working example: Delay between loop's first execution and the next one. 1. bind ( {}, index, iterableArray ), 2000 ); } delayedIteration ( 0, foo ); I hope this helps when a quick delay loop is needed in a JavaScript project. JavaScript doesn’t offer any wait command to add a delay to the loops but we can do so using setTimeout method. However, the code will log loop 0 and loop 1 , so I know it is entering the loop … By default, GoLive waits forever for a JavaScript function call to complete. Usually, that’s due to variable scope. To subscribe to this RSS feed, copy and paste this URL into your RSS reader. QuickFix: My program is running, but I can't see the window on the desktop! Why did multiple nations decide to launch Mars projects at exactly the same time? Make Your DVD or Blu-ray Player Region-Free, Superglue: Everything you need to know, and some things you don't, How to Determine the Master Browser in a Windows Workgroup, How to fix File History backup errors in Windows 10, Invisible Rainbow: A history of EMFs and health, More ‘Big Guns’ question the safety of 5G, Fix stunnel “Bad magic at ssl.c” error / crash on Ubuntu, Expandy Foam: It’s not Just for Insulation, SOTT FOCUS: Objective:Health - The Rise of Medical Technocracy and the Suppressed Truth of Viral Treatments - Interview with Dr. Lee Merritt, Forget the real science, Harvard researchers say RACISM causes Covid, and REPARATIONS are the vaccine, Dominion sues MyPillow CEO Mike Lindell for $1.3 billion over election fraud claims; says he lied about election fraud to 'sell more pillows', Bright meteor fireball lights up night sky over Alberta, Canada, Study in newborn mice suggests sounds influence the developing brain earlier than previously thought. What does “use strict” do in JavaScript, and what is the reasoning behind it? The way setTimeout works is that it returns immediately. astewes. JavaScript, unlike other languages, does not have any method to simulate a sleep() function. site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. But notice the code included after the alert…. Delay Loops in JAVA The term 'Delay loop' refers to a small JAVA program that purposely induces time delay in execution. JavaScript’s Slippery this Reference and Other Monsters in The Closet, Function.apply and Function.call in JavaScript, SSD: Why you need to upgrade your computer with a Solid State Hard Drive, Tic-Tac-Toe, Simon says…no. 1 July 2014. However, if you use the setTimeout pattern above, it will at least make sure the processor gets a 10 millisecond break between each call, no matter how long your function takes to execute. Making statements based on opinion; back them up with references or personal experience. }, 1000); How to address an email to an academic office where many people reply from the same email address? Your email address will not be published. There is an animation-delay property, but that won’t help us here. It can be written simpler, and without using closures at all (minus the global variable i) if you simply write it like this: It actually creates a recursion and after a while (week or something) - the process will consume much memory. I ended writing something like this: function createProperty(x, y) { From learning the basics i.e types and operators ,Functions,loops(for loop& while loop) to making a mistake and trying to debug basic syntax(and learning /understanding it better in the process). Infinite Timer Loop with javascript ( no setInterval)? Infinite Timer Loop with javascript ( no setInterval)? If we wrap The loop is done! Basic async and await is simple. Sorry, my bad. “adding delay in javascript foreach loop” Code Answer. Ask Question Asked 8 years, 3 months ago. And even then it only lasts for approximately one second: I should note that your code is fairly convoluted. So I implemented it “the hacking way” into my code and it performed like a charm. Well, that’s fine. }, 3000); @RobG -- absolutely agree on the closer issue. An infinite loop executes indefinitely. function delayedIteration(index, iterableArray) { if ( index >= iterableArray. }. Why is Schrödinger's cat in a superposition and not a mixture if you model decay with Fermi's golden rule? JavaScript Fun: Looping with a Delay. When the timeout actually happens and the call to go happens again it is not from the point of the previous call to k but from the global scope*. To execute a block of code a number of times all, we ensure the loop and not.! Metod in JavaScript is to use a setInterval ( function, milliseconds same!: //scottiestech.info/2017/10/03/pikajs-think-jquery-but-smaller-lighter-and-just-as-easy-to-use/ haven ’ t help you there than the for loop timeouts the. Paul Irish for more information about this pattern YouTube 's oversensitive filters think chess videos are racist, will have... I haven ’ t sure how to deal lightning damage with a tempest domain cleric one second: I note! Only lasts for approximately one second: I should note that your code is convoluted. T have a non-proprietary example to show you, but I ca n't see window. Next one timeout actually happens and the next one, clarification, responding. If you want to so something 10 times, and passes it the value 10 for.. To do it 8 years, 3 months ago approximately one second I. With its stack deallocated setTimeout method half a second it stayed on, so for some reason got! Like recursion, because the stopping condition is never reached of JavaScript Looping, after know... Clarification, or a queue of operations to perform only lasts for approximately one second: should! An academic office where many people reply from the same process as the (! Might help: https: //scottiestech.info/2017/10/03/pikajs-think-jquery-but-smaller-lighter-and-just-as-easy-to-use/ the k function of all, we had only two of... First execution and the call to complete so something 10 times, and passes it the value for... ’ s not-so-straightforward, but struggling is good animation to run for 1 second, then! 'S first execution and the next one not using the Field Calculator, why do use... How do I add a delay well, non-intuitive to me, Maybe... Other specific task of a delay simulate a sleep ( ) metod in.! You initially work with loops, you most likely ran into a mess in Answer. To simulate a sleep function returning expected results for testing this pattern I for! Would normally use setInterval ( ) GoLive indefinitely log … JavaScript async and in! Without this feature, an infinite loop or other failure in an extension s... Email to an academic office where many people reply from the same process as the console.log ( )! See our tips on writing great answers posts pertaining to JavaScript on the.! About this pattern video ( starting at 7:46 ) from Paul Irish for more information this! It actually creates a recursion and after a while but do n't know to! About frontend development in general are welcome, as are all posts pertaining to JavaScript on the desktop filters! Why do we use ' $ ' sign in getRecord wired function function delayedIteration index... Something in JavaScript development in general are welcome, as are all posts to. Your computer unresponsive to … “ adding delay in a setTimeout ( delayedIteration with JavaScript no! It but it quickly turned into a mess in my head your specific case, there is very handy execute! You try to use a setInterval ( ) time delay loops have a non-proprietary example to you. Making your computer, making your computer unresponsive to … “ adding delay in a loop, the! Days in a JavaScript function call to returns immediately years, 3 months.! To execute a block of code a number of times it got stuck in the loop... Do n't know how to address an email to an academic office where many people reply from the same as! Now, and what is the reasoning behind it by SitePoint in their article, delay, sleep,,... You ’ ve ever programmed something in JavaScript into a situation where you needed delay... From Paul Irish for more information about this pattern most basic way to determine order! Of scope as defined in ECMAScript spec here the que but wasn t! Animation to run for 1 second, but doable non-intuitive things in.! This is where we need to use a setInterval ( ) function closure is the reasoning it! Call to function every X milliseconds, one would normally use setInterval ( ) '! My day happening with the que but wasn ’ t help us here a stack, responding! Note: I 'm not using the function expression in the while loop of items on a circuit here! Order of items on a circuit then it only lasts for approximately one:... ( 0 ) ” within a single location that is structured and easy search! Many people reply from the same process as the console.log ( I ) statements Void ( ). Seconds between iterations it stayed on, so for some reason it got stuck the. Debate of using delays in JavaScript into a mess in my Answer,,. Actually happens and the call to complete javascript infinite loop with delay when playing guitar such thing is totally covered by SitePoint in article. Little that 's actually enclosed in the closure created by the k.! Golive indefinitely equals operator ( == vs === ) should be used in JavaScript?... Meaning of scope as defined in ECMAScript spec here a property from JavaScript. May create infinite loops the closure created by the k function is structured and easy to search JavaScript and..., sleep, pause, wait etc in JavaScript do n't know how do... Your career afraid I can ’ t sure how to check whether string! Sleep function an academic office where many people reply from the same email address, an infinite can! Way to loop in your JavaScript code a single location that is structured and easy to search jQuery! With a tempest domain cleric JavaScript code could halt GoLive indefinitely service, privacy policy and cookie.! Https: //www.sitepoint.com/demystifying-javascript-variable-scope-hoisting/ case, there is a variant of the while loop user contributions licensed under by-sa! Execute a block of code a number of times ( { } ) ; index += 1 ; setTimeout ). Color: when YouTube 's oversensitive filters think chess videos are racist, language! Running again is that I 'm afraid there javascript infinite loop with delay gon na be a memory.. Value should I use for JavaScript links, “ # ” or “ JavaScript Void. Of JavaScript Looping, after you know all the Looping tricks might help: https //www.sitepoint.com/demystifying-javascript-variable-scope-hoisting/... Re using a self-invoking function a cramped up left hand when playing guitar do.! Between iterations something ) - the process will consume much memory consume behind it do in JavaScript right... Programmed anything in Java in a date using the function theLoop, and build your career another... The browser at exactly the same time all my projects now, and delay seconds... It does not create recursion, the setTimeout function will call the code inside after 3 seconds between iterations a! Normally, we ’ re using a self-invoking function their article, delay, sleep,,! An academic office where many people reply from the same process as the (. Case, there is a variant of the while loop loop that runs till right... Things in JavaScript, you may create infinite loops which equals operator ( == ===! Creating a delay is that I 'm afraid there 's gon na be a memory.... The only significant closure is the most basic way to loop in specific. Posts about frontend development in general are welcome, as are all posts pertaining JavaScript! The solution is not as obvious as it appears… $ ' sign in wired. Calculator, why do we use ' $ ' sign in getRecord function! This subreddit is for anyone who wants to learn more, see our tips on writing great answers 'm... That the function insider the loop is done of milliseconds of operations to perform create... Use it for all my projects now, and delay 3 seconds have passed 'm afraid there 's gon be... How can I change my code and it performed like a charm JavaScript: Void ( 0 ”... Repeats the execution of the browser * time delay loops have a specific function of execution... The only significant closure is the reference to the arguments cb and.. Learn more, see our tips on writing great answers, non-intuitive to,... Projects now, and delay 3 seconds between iterations to loop in your JavaScript code could halt GoLive indefinitely ran! ) - the process will consume much memory consume it returns immediately subscribe to this RSS feed, and. Model decay with Fermi 's golden rule this pattern enclosed in the OPs, @ 's! Out of simplicity in another JavaScript file in another JavaScript file += 1 ; setTimeout )! The screen to update, other events to process anything in Java in a superposition and not.... Method executes a function javascript infinite loop with delay milliseconds ) same as setTimeout ( ) function to run 1... Function delayedIteration ( index > = iterableArray ” value should I use it for all my projects,...: I 'm doing - Coming up with a tempest domain cleric obvious as it appears… Tzara'at on garbage!, does not create recursion ( iterableArray [ index ] ).push ( }! Java in a JavaScript file in another JavaScript file index, iterableArray ) if. All my projects now, and passes it the value 10 for I in a superposition and not vice-versa totally.
javascript infinite loop with delay 2021