前端实现流式显示文本

发布时间 2023-11-15 15:45:08作者: 焦廉琨
<template>
  <div>
    <h1>{{ typedText }}</h1>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const text = 'Hello, World!';
const typedText = ref('');

onMounted(() => {
  let index = 0;
  const interval = setInterval(() => {
    typedText.value += text[index];
    index++;
    if (index === text.length) {
      clearInterval(interval);
    }
  }, 100);
});
</script>