函数返回未定义

我试图弄清楚为什么我的函数中出现未定义的错误。


我想做的是从逐个显示每个单词,input1直到input2使用split和array。


当按下该按钮时,它将起作用,并且文本从input1到可以循环显示input2。


但是,如果删除或添加了文本,input1则返回undefined。


var tagChanger = null,

  tagLength = 0,

  tagMaxLength = 0;


function changer() {

  clearInterval(tagChanger);

  if (tagChanger !== null) {

    tagChanger = null;

  } else {

    tagChanger = setInterval(function() {

      var tag = document.getElementById('input1').value,

        Name = tag.split(" ")[tagLength];

      document.getElementById('input2').value = Name;


      tagMaxLength = tag.split(" ").length - 1;

      if (tagMaxLength == tagLength) {

        tagLength = 0;

      } else {

        tagLength++;

      }


    }, 500);

  }

}


var button = document.getElementById("button");

button.addEventListener("click", changer);

input 1: &nbsp; <input type="text" id="input1" value="I HOPE SOMEONE CAN HELP ME FIX THIS">

<br><br> input 2: &nbsp; <input type="text" id="input2">

<br>

<br>

<button id="button" style="width: 30%;">BUTTON</button>


拉丁的传说
浏览 259回答 2
2回答

泛舟湖上清波郎朗

只需添加一个foreach循环并通过setTimeout类似的函数延迟它function changer() {&nbsp; var tag = document.getElementById('input1').value&nbsp; var Name = tag.split(" ");&nbsp; Name.forEach((sr, index) => {&nbsp; &nbsp; setTimeout(() => document.getElementById("input2").value = sr, 500 * index);&nbsp; });}var button = document.getElementById("button");button.addEventListener("click", changer);input 1: &nbsp; <input type="text" id="input1" value="DONT WORRY, THE HELP HAS ARRIVED"><br><br> input 2: &nbsp; <input type="text" id="input2"><br><br><button id="button" style="width: 30%;">BUTTON</button>另外,继续通过索引增加循环的持续时间,根据您的方便更改值

BIG阳

Name如果tag变量具有值,则添加时无需检入即可。这是你的问题var tagChanger = null,&nbsp; tagLength = 0,&nbsp; tagMaxLength = 0;function changer() {&nbsp; clearInterval(tagChanger);&nbsp; if (tagChanger !== null) {&nbsp; console.log("hi")&nbsp; &nbsp; tagChanger = null;&nbsp; } else {&nbsp; &nbsp; tagChanger = setInterval(function() {&nbsp; &nbsp; &nbsp; var tag = document.getElementById('input1').value&nbsp; &nbsp; &nbsp; if(tag != ''){&nbsp; &nbsp; &nbsp; &nbsp; var Name = tag.split(" ")[tagLength];&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('input2').value = Name;&nbsp; &nbsp; &nbsp; &nbsp; tagMaxLength = tag.split(" ").length - 1;&nbsp; &nbsp; &nbsp; &nbsp; if (tagMaxLength == tagLength) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tagLength = 0;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tagLength++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; tagLength = 0;&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('input2').value =""&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }, 500);&nbsp; }}var button = document.getElementById("button");button.addEventListener("click", changer);input 1: &nbsp; <input type="text" id="input1" value="I HOPE SOMEONE CAN HELP ME FIX THIS"><br><br> input 2: &nbsp; <input type="text" id="input2"><br><br><button id="button" style="width: 30%;">BUTTON</button>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript