Превратить случайный массив в последовательный массив - PullRequest
0 голосов
/ 23 октября 2019

У меня есть фрагмент кода для отображения эффекта ввода разностных предложений, но они рандомизированы ..

Это для моего личного сайта, но, к сожалению, я не очень хорош с JS

let typed = "";
const element = document.querySelector(".typity");

function startType(pun, index) {
  if (index < pun.length) {
    typed += pun.charAt(index);
    element.innerHTML = typed;
    index++;
    setTimeout(function() {
      startType(pun, index);
    }, 50);
  } else {
    setTimeout(function() {
      element.classList.add("highlight");
    }, 1000);

    setTimeout(function() {
      element.classList.remove("highlight");
      typed = "";
      element.innerHTML = typed;
      startType(getRandomPun(), 0);
    }, 2500);
  }
}

function getRandomPun() {
  const puns = [
    "A backwards poet writes inverse.",
    "A bicycle can't stand on its own because it's two-tired.",
    "A book just fell on my head. I've only got my shelf to blame.",
    "Yesterday I swallowed food coloring, I dyed a little on the inside."
  ];
  const index = Math.floor(Math.random() * puns.length);

  return puns[index];
}

startType(getRandomPun(), 0);

Я бы хотел, чтобы они отображались 1 к 1 по порядку, а затем снова возвращались к 1-му

1 Ответ

0 голосов
/ 23 октября 2019

Вы можете использовать js liberary , известный как vara.js

<a href="https://codepen.io/umer2001uf/pen/yLLMPKG">here</a>
...