简体   繁体   中英

How can I infinitely print a JS variable?

I am trying to write a program for my website where you can see a program "infinitely" print text. I want it to create a random variable, it doesn't matter that it is just random gibberish, and then write it out on the site itself. Right now I have created a piece of code that can create the gibberish, but I'm struggling with the printing part. I want it to show the previously generated gibberish as well, preferably on the same line. As an example:

  1. gibberish = wasd
  2. print gibberish
  3. generate the new gibberish
  4. combine gibberish 1 and gibberish 2
  5. print the combined gibberish
  6. repeat!

I have tried multiple pieces of code but I couldn't figure it out. It is probably a very simple loop so sorry in advance -_-

This is my latest attempt:

 function gibberish() { var randomsequence = ''; var followup = ''; function makeid(length) { var output = ''; var letters = 'abcdefghijklmnopqrstuvwxyz'; var charactersLength = letters.length; for ( var i = 0; i < length; i++ ) { output += letters.charAt(Math.floor(Math.random() * charactersLength)); } return output; } var monkeyoutput1 = ''; var stringoutput = monkeyoutput1 + makeid(Math.floor((Math.random() * 26) + 1)); monkeyoutput1 = stringoutput; document.getElementById("monkeybox").innerHTML = monkeyoutput1; //document.getElementById("monkeybox").innerHTML = makeid(Math.floor((Math.random() * 26) + 1)); } setInterval(gibberish, 1000);
 <div> <h1> text </h1><p id="monkeybox"> placeholder text</p> </div>

I've just added the innerHTML before the new text so it appends it

 function gibberish() { var randomsequence = ''; var followup = ''; function makeid(length) { var output = ''; var letters = 'abcdefghijklmnopqrstuvwxyz'; var charactersLength = letters.length; for ( var i = 0; i < length; i++ ) { output += letters.charAt(Math.floor(Math.random() * charactersLength)); } return output; } var monkeyoutput1 = ''; var stringoutput = monkeyoutput1 + makeid(Math.floor((Math.random() * 26) + 1)); monkeyoutput1 = stringoutput; document.getElementById("monkeybox").innerHTML = document.getElementById("monkeybox").innerHTML + monkeyoutput1; //document.getElementById("monkeybox").innerHTML = makeid(Math.floor((Math.random() * 26) + 1)); } setInterval(gibberish, 1000);
 <div> <h1> text </h1><p id="monkeybox"> placeholder text</p> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM