ncc1701d Posted March 27, 2016 Share Posted March 27, 2016 I am total beginner so please dont be mean or make fun but i am having dificulty understanding this i have this basic script I run in Stellatium that uses some javascripts functions but not all of them for scripting. for (i = 0; i < 5; i++){ core.debug( + i)} it produces: 01234 using only for loops or while loops how can I make it repeat over and over indefinatly ?1234 01234 01234 Link to comment Share on other sites More sharing options...
davej Posted March 27, 2016 Share Posted March 27, 2016 Do you want it to loop forever? while(true){ for(var i=0 ; i<5 ; i++){ console.log(i); } } ...of course the browser might not be happy with this, because Javascript is intended to be event-driven and not a continuous user of processor time. Link to comment Share on other sites More sharing options...
Ingolme Posted March 27, 2016 Share Posted March 27, 2016 Having it continue indefinitely would crash the browser, you should put a limit on how many times it displays a number. The way to do it without nested loops would be like this: var amount = 20; // Show 20 numbers var highestValue = 5; // Show numbers between 0 and 5 for(var num = 0, counter = 0; counter < amount; counter++, num++) { if(num > highestValue) { num = 0; } console.log(num); } Link to comment Share on other sites More sharing options...
ncc1701d Posted March 28, 2016 Author Share Posted March 28, 2016 thank you Devoted and FoxyMod Devoted..yes yours did crash but was still helpful FoxyMod yours worked as expected Foxy Mod I would be interested in the nested version to see how that is done as well if you would like to share that with me that would be helpfull as well. Thank you. Link to comment Share on other sites More sharing options...
Ingolme Posted March 28, 2016 Share Posted March 28, 2016 You should understand by now how loops work, you probably can figure it out on your own. Here's a version with nested loops. var times = 4; // Display the sequence four times var highestValue = 5; // Show numbers between 0 and 5 for(counter = 0; counter < times; counter++) { for(var num = 0; num <= highestValue; num++) { console.log(num); } } Link to comment Share on other sites More sharing options...
davej Posted March 29, 2016 Share Posted March 29, 2016 One approach that you could play with would be to not run such a loop continuously, but run it in a periodic manner... var si = setInterval(myLoop, 200); function myLoop() { for(var i=0 ; i<5 ; i++){ console.log(i); } } ...and there is also the newest approach... http://www.w3schools.com/html/html5_webworkers.asp Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now