JavaScript-未捕获的TypeError:totype [totypeIndex]未定义

阿迪亚·米什拉(Aditya Mishra)

我下面一个教程和完成了教程和取得的项目,但一段时间后,我的JS显示了一个错误Uncaught TypeError: totype[totypeIndex] is undefined,当我试图登录的类型,totype[totypeIndex]它呈先String,然后过一段时间后它显示undefined

我的代码:

const typedSpan = document.querySelector(".typed") // Storing the Span of HTML of Class 'typed' for changing the text
const totype = ["Fun", "Hard", "Love"] // Array in which all of the words which have to be typed are stored

const delayTyping_char = 200; // 200ms will be waited before next Character of the Text is typed
const delayErasing_text = 150; // 100ms will be waited before removing the next character
const delayTyping_text = 3000; // 2500ms will be waited before everything is erased & next text is typed

let totypeIndex = 0; // Index of The text which is being typed
let charIndex = 0; // Index of The Character which is being typed


function typeText() {
  if (charIndex < totype[totypeIndex].length) {
    typedSpan.textContent += totype[totypeIndex].charAt(charIndex); // Value of The Span in HTML is = the Character at the index of charIndex of the Text which is being typed
    charIndex++; // Adding 1 to charIndex
    setTimeout(typeText, delayTyping_char); // Calling typeText Until the charIndex is greater than the lenght of the Text which is being typed
  } else {
    setTimeout(eraseText, delayTyping_text); // if charIndex is Greater than lenght of the text which is being type then after the time setted (delayTyping_text) erase function will be called
  }
}

function eraseText() {
  if (charIndex > 0) {
    typedSpan.textContent = totype[totypeIndex].substring(0, charIndex - 1); // substring(0, charIndex-1) here charIndex-1 is saying to select the text leaving one of last text behind
    charIndex = charIndex - 1; // subtracting 1 from charIndex
    setTimeout(eraseText, delayErasing_text); // Will call eraseText() Function until the charIndex is not equal to 0
  } else {
    totypeIndex++; // If everything is erased then the totypeIndex will be increased and next text in Array (totype) will be typed

    if (totypeIndex > totype.length) // If totypeIndex is equal to or greater than the number of text to be typed then
      totypeIndex = 0; // totypeIndex will be 0 so that we can start from the first text in the array (toType) and
    setTimeout(typeText, delayTyping_text); // after some delay presetted (delayTyping_text) typeText() Function is called
  }
}

window.onload = function() {
  setTimeout(typeText, delayTyping_text);
} // typText() Function is called when everything is loaded
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background-color: #111111;
  font-family: monospace;
  color: #ffffff;
  text-align: center;
}

.wrapper {
  height: 100vh;
  width: auto;
  display: flex;
  justify-content: center;
  align-items: center;
}

.effect-wrapper {
  text-align: center;
  font-weight: normal;
}

.typed {
  font-weight: bold;
  color: #00bdff;
}

.cursor {
  display: inline-block;
  background-color: #b0ff95;
  animation: blinker 800ms infinite;
}

.cursor.typing-true {
  animation: none;
}

@keyframes blinker {
  0% {
    background-color: #b0ff95;
  }
  50% {
    background-color: transparent;
  }
  100% {
    background-color: #b0ff95;
  }
}
<div class="wrapper">
  <!-- Wrapper Start -->
  <h1 class="effect-wrapper">Coding is <span class="typed"></span> <span class="cursor">&nbsp;</span></h1>
</div>
<!-- Wrapper End -->

请告诉我错误是什么以及如何解决?

提前致谢

伯勒姆·索利曼

错字在这里

if (totypeIndex > totype.length) // If totypeIndex is equal to or greater than the number of text to be typed then

您必须将此语句设置为更高或相等,因此请使用>=而不是>

const typedSpan = document.querySelector(".typed") // Storing the Span of HTML of Class 'typed' for changing the text
const totype = ["Fun", "Hard", "Love"] // Array in which all of the words which have to be typed are stored

const delayTyping_char = 200; // 200ms will be waited before next Character of the Text is typed
const delayErasing_text = 150; // 100ms will be waited before removing the next character
const delayTyping_text = 3000; // 2500ms will be waited before everything is erased & next text is typed

let totypeIndex = 0; // Index of The text which is being typed
let charIndex = 0; // Index of The Character which is being typed


function typeText() {
  if (charIndex < totype[totypeIndex].length) {
    typedSpan.textContent += totype[totypeIndex].charAt(charIndex); // Value of The Span in HTML is = the Character at the index of charIndex of the Text which is being typed
    charIndex++; // Adding 1 to charIndex
    setTimeout(typeText, delayTyping_char); // Calling typeText Until the charIndex is greater than the lenght of the Text which is being typed
  } else {
    setTimeout(eraseText, delayTyping_text); // if charIndex is Greater than lenght of the text which is being type then after the time setted (delayTyping_text) erase function will be called
  }
}

function eraseText() {
  if (charIndex > 0) {
    typedSpan.textContent = totype[totypeIndex].substring(0, charIndex - 1); // substring(0, charIndex-1) here charIndex-1 is saying to select the text leaving one of last text behind
    charIndex = charIndex - 1; // subtracting 1 from charIndex
    setTimeout(eraseText, delayErasing_text); // Will call eraseText() Function until the charIndex is not equal to 0
  } else {
    totypeIndex++; // If everything is erased then the totypeIndex will be increased and next text in Array (totype) will be typed

    if (totypeIndex >= totype.length) // If totypeIndex is equal to or greater than the number of text to be typed then
      totypeIndex = 0; // totypeIndex will be 0 so that we can start from the first text in the array (toType) and
    setTimeout(typeText, delayTyping_text); // after some delay presetted (delayTyping_text) typeText() Function is called
  }
}

window.onload = function() {
  setTimeout(typeText, delayTyping_text);
} // typText() Function is called when everything is loaded
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background-color: #111111;
  font-family: monospace;
  color: #ffffff;
  text-align: center;
}

.wrapper {
  height: 100vh;
  width: auto;
  display: flex;
  justify-content: center;
  align-items: center;
}

.effect-wrapper {
  text-align: center;
  font-weight: normal;
}

.typed {
  font-weight: bold;
  color: #00bdff;
}

.cursor {
  display: inline-block;
  background-color: #b0ff95;
  animation: blinker 800ms infinite;
}

.cursor.typing-true {
  animation: none;
}

@keyframes blinker {
  0% {
    background-color: #b0ff95;
  }
  50% {
    background-color: transparent;
  }
  100% {
    background-color: #b0ff95;
  }
}
<div class="wrapper">
  <!-- Wrapper Start -->
  <h1 class="effect-wrapper">Coding is <span class="typed"></span> <span class="cursor">&nbsp;</span></h1>
</div>
<!-- Wrapper End -->

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

JavaScript未捕获ReferenceError:未定义jQuery;未捕获的ReferenceError:未定义$

来自分类Dev

Javascript regex .test()“未捕获的TypeError:未定义不是函数”

来自分类Dev

JavaScript错误“未捕获的TypeError:无法调用未定义的方法'push'” D3.js

来自分类Dev

Javascript | 未捕获的TypeError:无法设置未定义的属性“颜色”

来自分类Dev

JavaScript代码块中的“未捕获的TypeError:未定义不是函数”

来自分类Dev

JavaScript-未捕获的TypeError:无法读取未定义的属性“搜索”

来自分类Dev

Javascript,未捕获的TypeError:无法读取未定义的属性“单元格”

来自分类Dev

javascript抛出未捕获的TypeError:填充多维数组时无法设置未定义的属性“ 0”

来自分类Dev

jQuery / Javascript-未捕获的TypeError:未定义不是一个函数

来自分类Dev

javascript:未捕获的TypeError:无法读取未定义的属性“ Play”

来自分类Dev

Parse.com未捕获的TypeError:无法读取未定义JavaScript的属性'className'

来自分类Dev

JavaScript-未捕获的TypeError:无法读取未定义的属性“搜索”

来自分类Dev

Javascript | 未捕获的TypeError:无法设置未定义的属性“颜色”

来自分类Dev

javascript“未捕获的TypeError:无法读取未定义的属性'textfield'”

来自分类Dev

Javascript:简单函数-未捕获的TypeError:无法读取未定义的属性“ length”

来自分类Dev

phaser.js,javascript,未捕获的TypeError:无法读取未定义的属性“ forEach”

来自分类Dev

Javascript Handsontable-未捕获的TypeError:无法读取未定义的属性'insertBefore'

来自分类Dev

JavaScript-未捕获的ReferenceError:未定义KEY

来自分类Dev

Javascript未捕获ReferenceError:未定义增量

来自分类Dev

“未捕获的ReferenceError:未定义myVar” OOP Javascript

来自分类Dev

HTML和JavaScript未捕获ReferenceError:未定义addTable

来自分类Dev

未捕获的ReferenceError:未定义$(PHP中的JavaScript / HTML)

来自分类Dev

JavaScript无法从localStorage捕获未定义

来自分类Dev

如何在JavaScript中编写CSS代码?(未捕获的TypeError:无法设置未定义的属性“ height”)

来自分类Dev

使用人脸api的javascript中的人脸识别“未捕获(承诺)TypeError:无法读取未定义的属性'descriptor'”

来自分类Dev

jquery.min.js:2未捕获的TypeError:无法读取未定义的属性'replace'(javascript)(laravel)

来自分类Dev

在对象中调用方法时,Javascript中出现“未捕获的TypeError:未定义不是函数”的错误消息

来自分类Dev

未定义:未删除javascript数组

来自分类Dev

Javascript承诺给TypeError函数未定义

Related 相关文章

  1. 1

    JavaScript未捕获ReferenceError:未定义jQuery;未捕获的ReferenceError:未定义$

  2. 2

    Javascript regex .test()“未捕获的TypeError:未定义不是函数”

  3. 3

    JavaScript错误“未捕获的TypeError:无法调用未定义的方法'push'” D3.js

  4. 4

    Javascript | 未捕获的TypeError:无法设置未定义的属性“颜色”

  5. 5

    JavaScript代码块中的“未捕获的TypeError:未定义不是函数”

  6. 6

    JavaScript-未捕获的TypeError:无法读取未定义的属性“搜索”

  7. 7

    Javascript,未捕获的TypeError:无法读取未定义的属性“单元格”

  8. 8

    javascript抛出未捕获的TypeError:填充多维数组时无法设置未定义的属性“ 0”

  9. 9

    jQuery / Javascript-未捕获的TypeError:未定义不是一个函数

  10. 10

    javascript:未捕获的TypeError:无法读取未定义的属性“ Play”

  11. 11

    Parse.com未捕获的TypeError:无法读取未定义JavaScript的属性'className'

  12. 12

    JavaScript-未捕获的TypeError:无法读取未定义的属性“搜索”

  13. 13

    Javascript | 未捕获的TypeError:无法设置未定义的属性“颜色”

  14. 14

    javascript“未捕获的TypeError:无法读取未定义的属性'textfield'”

  15. 15

    Javascript:简单函数-未捕获的TypeError:无法读取未定义的属性“ length”

  16. 16

    phaser.js,javascript,未捕获的TypeError:无法读取未定义的属性“ forEach”

  17. 17

    Javascript Handsontable-未捕获的TypeError:无法读取未定义的属性'insertBefore'

  18. 18

    JavaScript-未捕获的ReferenceError:未定义KEY

  19. 19

    Javascript未捕获ReferenceError:未定义增量

  20. 20

    “未捕获的ReferenceError:未定义myVar” OOP Javascript

  21. 21

    HTML和JavaScript未捕获ReferenceError:未定义addTable

  22. 22

    未捕获的ReferenceError:未定义$(PHP中的JavaScript / HTML)

  23. 23

    JavaScript无法从localStorage捕获未定义

  24. 24

    如何在JavaScript中编写CSS代码?(未捕获的TypeError:无法设置未定义的属性“ height”)

  25. 25

    使用人脸api的javascript中的人脸识别“未捕获(承诺)TypeError:无法读取未定义的属性'descriptor'”

  26. 26

    jquery.min.js:2未捕获的TypeError:无法读取未定义的属性'replace'(javascript)(laravel)

  27. 27

    在对象中调用方法时,Javascript中出现“未捕获的TypeError:未定义不是函数”的错误消息

  28. 28

    未定义:未删除javascript数组

  29. 29

    Javascript承诺给TypeError函数未定义

热门标签

归档