代码实现
HTML
<div class="main"></div>
JS
let str = { msg: "", len: function () { return this.msg.length; }, index: 0, speed: 200, //控制打字时间(ms) type: function () { let t = this; document.querySelector(".main").innerHTML = t.msg.substring(0,t.index); if (t.index == t.len()) { t.index = 0; clearTimeout(timer); } else { t.index++; timer = setTimeout(function () { t.type(); }, this.speed); } }, }; let msg = "输入你想说的任何语言"; str.msg = msg; str.type();