JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Useful Array Methods

re22ui
re22ui
4,741 Points

I am not exactly sure why the instructor's HTML auto refreshes. I have to type quit in the prompt dialog for result.

I have decided to keep going. I am curious if this happens to anyone else?

Resh Galberth:

There's a little arrow to the right of the web page URL that looks like it's curving to the right at the top of the web page. He's clicking on it to refresh his page. If you hover over the curling arrow it will say "Refresh (F5)" (Windows 7).

re22ui
re22ui
4,741 Points

I am not talking about refreshing a page I am talking about the fact that his answer (i.e. we have eggs in stock) pop up as soon as he puts in a search. The prompt dialog box is still up at the same time he is searching the program for new items. My screen shows the dialog prompt and allows me to enter a search, my browser window will be blank with no HTML search results (i.e. no answers or text ) until I type quit. Then search results appear and the dialog box is gone. Not a big deal, just curiosity.

Resh Galberth:

You didn't specify what you meant by "refresh" in your original post. It's best to refer to the lesson you have a question about, too.

Resh, I had the same thing going on with that lesson. I never figured it out. The code worked it just didn't do that thing that the instructor has going on. I'll be curious if anyone has the answer or if you and I are the only ones that ever had this happen.

re22ui
re22ui
4,741 Points

John Larson, Thank you for the feedback. I am glad to know I am not alone. I may revisit this puzzle when I am done learning all I can about JS. Who knows, someone may show up with a solution. lets keep our fingers crossed! :-)

6 Answers

It actually works as in the video, within FireFox (at the time of this post, FF49). Chrome must have changed how it handled infinite loops.

Michael Liendo
Michael Liendo
15,324 Points

Ah. I see whatcha mean. I definitely learned something new while creating this codepen. So in a while(true) loop, the DOM never gets a chance to update. A workaround, to achieve the desired effect is to call setInterval()

example with jQuery:

<div id='content'></div>
function promptAndInsertContent() {
  var contentToInsert = prompt('Enter in some text yo!');
 //Press Cancel to quit
if(contentToInsert === null) {
//use clearInterval to exit the timer
   clearInterval(timer);
 }
 $('#content').text(contentToInsert);
}

//call the timer every 200 miliseconds
var timer = setInterval(promptAndInsertContent, 200); 

Working codepen: http://codepen.io/qctimes/pen/rrNNGy

Michael Liendo
Michael Liendo
15,324 Points

edit to include the codepen...I've never been good at formatting code lol

Ok, I get it, and then if I wanted the previous content to be added to you would append it and the list would grow and display each time you hit enter. I wish the teacher would have mentioned he had something going on that we didn't get to yet though.

Michael Liendo
Michael Liendo
15,324 Points

It's simply how his loop is constructed. I know the workspace files don't reflect it (as noted in other forum questions) but using a while loop you can update the DOM until "quit" is typed. Feel free to post your code if you need help, or see the related questions in the forum.

Michael Liendo

  • 'using a while loop you can update the DOM until "quit" is typed'.
  • That sounds intriguing. I can kinda see how that would be done, but I'd love to see a brief snippet of what that looks like.
Blake Schwartz
Blake Schwartz
4,282 Points

Same issue... odd that they don't mention it at all in the video. It's confusing.

Resh Galberth, john larson, Michael Liendo, cesaradames, & Blake Schwartz:

Don't forget to "Rate the Video" when you finish seeing it before you click the "Continue" button to go to the next course. That way the instructor gets direct feedback so the video can updated & improved. Let him or her know to be more specific & demonstrate on the video what they're doing & describe it in detail verbally. You can view the written transcript, too. It's easy for an instructor to assume you know what they're doing already. You can always stop the video & rewind it to go back to the part you didn't understand the first time. You can do that as many times as you need to do so. It's O.K. to slow down & not rush through the videos. Sometimes you catch the instructor's typographical errors that way, too. Sometimes the instructor corrects them in the video, & sometimes they forget to do so. Most of the time, they correct themselves in the video.