Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Introduction to Programming Control Structures Loops

Justine Jordan
Justine Jordan
111 Points

How do you stop an infinite loop? Tutorial crashed my browser (and this lesson).

Jim mentions that he's opened localhost to stop the infinite loop but I don't have a local server running (and in fact, setting up a local server is never explained nor is a requirement—at least so far—in this lesson).

I executed the loop and was unable to stop it. It crashed my browser and got my fans going pretty good.

7 Answers

Justine, my computer did the same thing - had to force quit and restart. Kinda wish the instructor had said "BTW, DO NOT TRY THIS AT HOME" for those of us who learn best by following along with the code on our own computers. (-:

Justine Jordan
Justine Jordan
111 Points

Totally agree! I lost my progress on the lesson, all the other pages I had open in other tabs, etc. Huge bummer—I would have appreciated a warning.

Hello Justine Jordan;

Let's say I want to print the phrase "Hello World!" ten times. This is how I'd do it using while loop

        var count = 0;

        while (count < 10) {
            document.write("Hello World!<br>");
            count++;    
        }

first I created a variable named count and assigned the value 0 to it

Then, the while loop will check if the value in "count" is less than 10. If the value is less than 10, then the code inside the while loop will be executed. Next, I'll increment the value in count by 1 using

count++;

OR

count = count + 1;

OR

count += 1;

All of them are correct.

Note if you don't increment the value in "count", you'll be stuck in an infinite loop.

I hope that helped

Justine Jordan
Justine Jordan
111 Points

Sorry, I meant how do you stop it in the browser once it's already been executed.

Not "how do you prevent an infinite loop in your program."

Oh my bad, I misunderstood you. I usually restart my browser. I use Force Quit in Mac

Hi Justine,

I recommend to use timer (SetTimeOut function) to implement your infinite loop.

The below link is an example:

http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_stop

Browsers will be dead if using 'for' loop or 'while' loop implements infinite loop. What will have to be done in this situation is just to close or Force Quit browsers.

Hope this is useful for you question.

Thanks!

Terence Chan
Terence Chan
11,867 Points

Hi Justine, the only thing you could do is quit or force quit your browser and start again. That part of the tutorial is to show you a loop could go on forever and will crash your browser. I just watched him do it but skipped doing that :)

N alway
N alway
23,524 Points

press (ctrl + shift + esc) if on windowsOS to open the task manager then right click your browser icon Chrome ,fireFox and then click 'End Task'.

Right before I hit refresh I told myself not to execute this because I knew it would crash my browser... And still I executed it... not sure when my hands had a mind of their own but yea I feel your pain. I just force closed my browser.

Darush Mozhdehi
Darush Mozhdehi
1,280 Points

you can close console (by clicking on x right side of console) and then reopen it by clicking on 'view' and then 'show console'.