首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用settimeout迭代数组

如何使用settimeout迭代数组
EN

Stack Overflow用户
提问于 2022-09-05 22:12:22
回答 1查看 46关注 0票数 -1

我试图让文本在数组中循环,并在屏幕上呈现CSS动画,并在每次更改中延迟一秒钟。我的可执行代码将给出我想要实现的目标。

HTML

代码语言:javascript
复制
 <div class="first">
   <div class="vert-line"></div>
    <p>Are you looking for a quick learner?</p>
  </div> 
  <div class="second">
   <div class="vert-line"></div>
    <p>Someone who is ever evolving?</p>
   </div>
   <div class="third">
     <div class="vert-line"></div>
       <p>Someone who is strives to improve their knowledge?</p>
    </div>
    <div class="fourth">
     <div class="vert-line"></div>
      <p>Someone who loves problem-solving and finding solutions?</p>
   </div>
   <div class="fifth">
     <div class="vert-line"></div>
       <p>Someone who has the tenacity to succeed?</p>
   </div>

JS

代码语言:javascript
复制
let first = document.querySelector(".first p")
let second = document.querySelector(".second p")
let third = document.querySelector(".third p")
let fourth = document.querySelector(".fourth p")
let fifth = document.querySelector(".fifth p")

let textArray =[first,second,third,fourth,fifth]
let textArrayShow = []

function AnimationTimer(){
  for(let i =0; i < textArray.length; i++){
    console.log(textArray[i])
  }

  textArray.forEach(() => {
    setTimeout(5000)
  });
}

AnimationTimer()

所以最终的结果是,它需要动画,当它滑动下来时,它会改变到数组中的下一个文本,它需要一遍又一遍地重复自己。

它意味着成为我这种人的卖点

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-06 06:10:58

所以如果我明白你想这么做

代码语言:javascript
复制
let textElement = document.querySelector("p")
let textArray =[
  "Are you looking for a quick learner?",
  "Someone who is ever evolving?",
  "Someone who is strives to improve their knowledge?",
  "Someone who loves problem-solving and finding solutions?",
  "Someone who has the tenacity to succeed?"
]
let index = 0
textElement.innerText = textArray[index]

setInterval(()=>{
  index = (index +1)%textArray.length
  textElement.innerText = textArray[index]
}, 1000)
代码语言:javascript
复制
<p></p>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73615076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档