reset password
Author Message
BernardBollinger
Posts: 13
Posted 23:25 Feb 13, 2017 |

Document.write isn't executed until the while loop is done, which wont help the user. How can I update the user with location/health/etc within the game loop using document.write?

amclees2
Posts: 12
Posted 23:38 Feb 13, 2017 |

If you call document.write immediately after changing the location/health it will appear on the page immediately. If you want to write to the top of the page so make newer text visible during alert/confirm/prompt popups you can do something like:

var text = "Text you want to appear";

document.body.innerHTML = text + document.body.innerHTML;

The above code is not particularly efficient but is probably the easiest way to write text to the top of the page. If you put the above code into a function and call the function whenever you update the location/health you can display the results immediately at the top of the page. You can also use the insertBefore method on document.body to add an element object to the front of the page.

If you try any of the above methods, you should put your script tag in the body of your HTML document. If you don't, your script will be loaded before the HTML body so document.body will be undefined and you will get an error.