简体   繁体   中英

Cannot read property 'length' of undefined ERROR on JavaScript

I created a Typewrite animation in JavaScript but there is one problem, my console just threw me the next error every time it resets: Uncaught TypeError: Cannot read property 'length' of undefined at StartTextAnimation. I hope you can help me to find the problem here. Thanks

<h1 class="typewrite fontrielsa" style="color: rgba(0,0,0,.5); font-weight: 300 !important; margin-bottom: 25px;"></h1>

<style>

.cursorType {
  border-right: .05em solid;
  animation: caret 1s steps(1) infinite;
}

@keyframes caret {
  50% {
    border-color: transparent;
  }
}


</style>

<script>

    document.addEventListener('DOMContentLoaded',function(event){
  // array with texts to type in typewriter
  var dataText = [ "MÁS DE 25 AÑOS DE EXPERIENCIA.", "EQUIPOS Y MATERIALES PARA LABORATORIO.", "DEPARTAMENTO DE INGENIERÍA.", "EMPRESA 100% MEXICANA."];

  // type one text in the typwriter
  // keeps calling itself until the text is finished
  function typeWriter(text, i, fnCallback) {
    // chekc if text isn't finished yet
    if (i < (text.length)) {
      // add next character to h1
     document.querySelector(".typewrite").innerHTML = text.substring(0, i+1) +'<span class="cursorType" aria-hidden="true"></span>';

      // wait for a while and call this function again for next character
      setTimeout(function() {
        typeWriter(text, i + 1, fnCallback)
      }, 100);
    }
    // text finished, call callback if there is a callback function
    else if (typeof fnCallback == 'function') {
      // call callback after timeout
      setTimeout(fnCallback, 5000);
    }
  }
  // start a typewriter animation for a text in the dataText array
   function StartTextAnimation(i) {
     if (typeof dataText[i] == 'undefined'){
        setTimeout(function() {
          StartTextAnimation(0);
        }, 1000);
     }
     // check if dataText[i] exists
    if (i < dataText[i].length) {
      // text exists! start typewriter animation
     typeWriter(dataText[i], 0, function(){
       // after callback (and whole text has been animated), start next text
       StartTextAnimation(i + 1);
     });
    }
  }
  // start the text animation
  StartTextAnimation(0);
});



</script>


The portion of code underneath the dataText[i] check is being run regardless if the undefined check passes or fails. You could try putting it into an if / else such as:

function StartTextAnimation(i) {
  if (typeof dataText[i] == 'undefined'){
    // check if dataText[i] exists
    setTimeout(function() {
      StartTextAnimation(0);
    }, 1000);
  } else if (i < dataText[i].length) {
    // text exists! start typewriter animation
    typeWriter(dataText[i], 0, function(){
      // after callback (and whole text has been animated), start next text
      StartTextAnimation(i + 1);
    });
  }
}

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