Невозможно прочесть свойство 'length' неопределенной ОШИБКИ на JavaScript - PullRequest
0 голосов
/ 18 июня 2020

Я создал анимацию Typewrite в JavaScript, но есть одна проблема, моя консоль выдает мне следующую ошибку каждый раз при сбросе: Uncaught TypeError: не удается прочитать свойство length of undefined в StartTextAnimation. Надеюсь, вы поможете мне найти здесь проблему. Спасибо

<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>


1 Ответ

0 голосов
/ 18 июня 2020

Часть кода под проверкой dataText[i] выполняется независимо от того, прошла ли неопределенная проверка или нет. Вы можете попробовать поместить его в if / else, например:

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);
    });
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...