javascript - How can I use document.write in setTimout without clearing previous text -


i want create delay between 2 document.writes. used settimeout so, once executes, writes on previous text. able display text, wait delay, display other text below without erasing previous text. code appending otherwise empty html file. also, haven't had success using <br> this.

var numofdice = prompt("how many dice?"); var numofsides = prompt("how many sides on these dice?");  var rolldice = function(numofdice) {     var rollresults = [];       (var = 0; < numofdice; i++) {         rollresults[i] = math.floor((math.random() * numofsides) + 1);     }      return rollresults; }  var printresults = function() {     var = 0;     while (i < rollresults.length - 1) {                 document.write(rollresults[i] + ", ");         i++;     }      document.write(rollresults[i]); }  alert("roll dice!");   var rollresults = rolldice(numofdice);  printresults(); settimeout(function() {document.write("these numbers...")}, 1000); 

first, take @ this answer why shouldn't using document.write.

then, after understand why using document.write bad practice, can replace element.appendchild.

for more information function, visit mozilla development network.

code example:

var numofdice = prompt("how many dice?"); var numofsides = prompt("how many sides on these dice?");  var rolldice = function(numofdice) {   var rollresults = [];     (var = 0; < numofdice; i++) {     rollresults[i] = math.floor((math.random() * numofsides) + 1);   }    return rollresults; }  var printresults = function() {   var = 0;   while (i < rollresults.length - 1) {     var node = document.createelement("p");     node.innerhtml = rollresults[i] + ", ";     document.queryselector("body").appendchild(node);     i++;   }   var node = document.createelement("p");   node.innerhtml = rollresults[i];   document.queryselector("body").appendchild(node); }  alert("roll dice!");  var rollresults = rolldice(numofdice);  printresults(); settimeout(function() {   var node = document.createelement("p");   node.innerhtml = "these numbers...";   document.queryselector("body").appendchild(node); }, 1000); 

note code show each rolled dice on new line, since i'm creating new p element every roll. if want them appear on same line, create 1 p element, , in while loop, append rollresult p elements `innerhtml´ .


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -