如何无限打印 JS 变量?

我正在尝试为我的网站编写一个程序,您可以在其中看到程序“无限”打印文本。我希望它创建一个随机变量,它只是随机的乱码并不重要,然后将其写在网站本身上。现在我已经创建了一段可以创建乱码的代码,但我在打印部分上遇到了困难。我希望它也显示之前生成的乱码,最好在同一行。举个例子:

  1. 胡言乱语=wasd

  2. 打印乱码

  3. 产生新的乱码

  4. 合并乱码 1 和乱码 2

  5. 打印组合后的乱码

  6. 重复!

我尝试了多段代码,但无法弄清楚。这可能是一个非常简单的循环,所以提前抱歉 -_-

这是我的最新尝试:

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>


繁星淼淼
浏览 36回答 1
1回答

SMILET

我刚刚在新文本之前添加了innerHTML,因此它会附加它function gibberish() {&nbsp; &nbsp; &nbsp; &nbsp; var randomsequence = '';&nbsp; &nbsp; &nbsp; &nbsp; var followup = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function makeid(length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var output&nbsp; = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var letters = 'abcdefghijklmnopqrstuvwxyz';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var charactersLength = letters.length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for ( var i = 0; i < length; i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output += letters.charAt(Math.floor(Math.random() * charactersLength));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return output;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var monkeyoutput1 = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var stringoutput = monkeyoutput1 + makeid(Math.floor((Math.random() * 26) + 1)) ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; monkeyoutput1 = stringoutput;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("monkeybox").innerHTML = document.getElementById("monkeybox").innerHTML + monkeyoutput1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //document.getElementById("monkeybox").innerHTML = makeid(Math.floor((Math.random() * 26) + 1)) ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }setInterval(gibberish, 1000);<div>&nbsp; &nbsp; <h1> text </h1><p id="monkeybox"> placeholder text</p></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript