【问题标题】:Stop auto type at last array [closed]在最后一个数组停止自动输入[关闭]
【发布时间】:2021-01-23 12:49:47
【问题描述】:

当到达最后一个数组时,我正试图停止擦除我的“自动键入的文本”。 我一直在谷歌上搜索,但找不到任何解决方案。

const typedTextSpan = document.querySelector(".typed-text");
const cursorSpan = document.querySelector(".cursor");
const textArray = ["reconnaitre un talent ?", "détécter une opportunité ?"];
const typingDelay = 170;
const erasingDelay = 100;
const newTextDelay = 2000;
let textArrayIndex = 0;
let charIndex = 0;

function type() {
  if (charIndex < textArray[textArrayIndex].length) {
    if (cursorSpan.classList.contains("typing")) cursorSpan.classList.add("typing");
    typedTextSpan.textContent += textArray[textArrayIndex].charAt(charIndex);
    charIndex++;
    setTimeout(type, typingDelay);
  } else {
    cursorSpan.classList.remove("typing");
    setTimeout(erase, newTextDelay);
  }
}

function erase() {
  if (charIndex > 0) {
    if (!cursorSpan.classList.contains("typing")) cursorSpan.classList.add("typing");
    typedTextSpan.textContent = textArray[textArrayIndex].substring(0, charIndex - 1);
    charIndex--;
    setTimeout(erase, erasingDelay);
  } else {
    cursorSpan.classList.remove("typing");
    textArrayIndex++;
    if (textArrayIndex >= textArray.length) textArrayIndex = 0;
    setTimeout(type, typingDelay + 1100);
  }
}

document.addEventListener("DOMContentLoaded", function() {
  if (textArray.length) setTimeout(type, newTextDelay + 250);
});
<div class="container" id="container">
  <h1>Savez-vous <span class="typed-text" id="typed-text"></span><span class="cursor" id="cursor">&nbsp;</span></h1>
</div>

【问题讨论】:

    标签: javascript arrays erase


    【解决方案1】:

    目前,您的函数不断相互调用,没有任何信号停止某处。您已经使用textArrayIndex 跟踪您的索引当前在textArray 中的位置。使用它来查看您的 type 函数的 else 语句中是否已达到结尾。

    if (textArrayIndex < textArray.length - 1) {
      setTimeout(erase, newTextDelay);
    }
    

    这里它检查当前索引是否低于数组的最后一个索引。如果是,它将继续调用erase 函数并在那里增加textArrayIndex1。否则当索引不低于最后一个索引时,意味着它是最后一个,它将什么都不做,从而打破循环。

    const typedTextSpan = document.querySelector(".typed-text");
    const cursorSpan = document.querySelector(".cursor");
    const textArray = ["reconnaitre un talent ?", "détécter une opportunité ?"];
    const typingDelay = 170;
    const erasingDelay = 100;
    const newTextDelay = 2000;
    let textArrayIndex = 0;
    let charIndex = 0;
    
    function type() {
      if (charIndex < textArray[textArrayIndex].length) {
        if (cursorSpan.classList.contains("typing")) cursorSpan.classList.add("typing");
        typedTextSpan.textContent += textArray[textArrayIndex].charAt(charIndex);
        charIndex++;
        setTimeout(type, typingDelay);
      } else {
        cursorSpan.classList.remove("typing");
        
        // Erase if the end has not yet been reached.
        if (textArrayIndex < textArray.length - 1) {
          setTimeout(erase, newTextDelay);
        }
      }
    }
    
    function erase() {
      if (charIndex > 0) {
        if (!cursorSpan.classList.contains("typing")) cursorSpan.classList.add("typing");
        typedTextSpan.textContent = textArray[textArrayIndex].substring(0, charIndex - 1);
        charIndex--;
        setTimeout(erase, erasingDelay);
      } else {
        cursorSpan.classList.remove("typing");
        textArrayIndex++;
        setTimeout(type, typingDelay + 1100);
      }
    }
    
    document.addEventListener("DOMContentLoaded", function() {
      if (textArray.length) setTimeout(type, newTextDelay + 250);
    });
    <div class="container" id="container">
      <h1>Savez-vous <span class="typed-text" id="typed-text"></span><span class="cursor" id="cursor">&nbsp;</span></h1>
    </div>

    【讨论】:

    • 嘿 :) 非常感谢,我专注于 textArray 的最后一个索引,哈哈。
    【解决方案2】:

    您需要在擦除函数中比较textArrayIndextextArray 的长度。请检查下面的代码。

    const typedTextSpan = document.querySelector(".typed-text");
    const cursorSpan = document.querySelector(".cursor");
    const textArray = ["reconnaitre un talent ?", "détécter une opportunité ?"];
    const typingDelay = 170;
    const erasingDelay = 100;
    const newTextDelay = 2000;
    let textArrayIndex = 0;
    let charIndex = 0;
    
    function type() {
      if (charIndex < textArray[textArrayIndex].length) {
        if (cursorSpan.classList.contains("typing")) cursorSpan.classList.add("typing");
        typedTextSpan.textContent += textArray[textArrayIndex].charAt(charIndex);
        charIndex++;
        setTimeout(type, typingDelay);
      } else {
        cursorSpan.classList.remove("typing");
        setTimeout(erase, newTextDelay);
      }
    }
    
    function erase() {
      // Check index
      if(textArrayIndex == textArray.length - 1) {
          // Stop Erase
          return false; 
      }
      if (charIndex > 0) {
        if (!cursorSpan.classList.contains("typing")) cursorSpan.classList.add("typing");
        typedTextSpan.textContent = textArray[textArrayIndex].substring(0, charIndex - 1);
        charIndex--;
        setTimeout(erase, erasingDelay);
      } else {
        cursorSpan.classList.remove("typing");
        textArrayIndex++;
        if (textArrayIndex >= textArray.length) textArrayIndex = 0;
        setTimeout(type, typingDelay + 1100);
      }
    }
    document.addEventListener("DOMContentLoaded", function() {
      if (textArray.length) setTimeout(type, newTextDelay + 250);
    });
    <div class="container" id="container">
      <h1>Savez-vous <span class="typed-text" id="typed-text"></span><span class="cursor" id="cursor">&nbsp;</span></h1>
    </div>

    【讨论】:

    • 您好,非常感谢。就像我在上一条评论中所说的那样,我专注于 textArray 的最后一个索引 :)
    猜你喜欢
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 2021-11-25
    相关资源
    最近更新 更多