vue3编写倒计时效果

说明:来自CSDN-问答板块,题主提问。

需求:如何通过表单控制倒计时开始时间,比如设定倒计时五分钟,循环几次,点击开始倒计时按钮,就让他从5分00秒,开始每秒减少,然后到0分00秒后,开始下一次循环,显示这是第几次循环,能不能点击开始倒计时,然后这个表单隐藏起来,还有就是计时结束之后,再设置计时得刷新才能正常。 

一、解决代码

<template>
  <div>
    <div v-if="showForm">
      循环的次数:<input type="number" v-model="cishu"><br>
      每次几分钟:<input type="number" v-model="timeone"><br>
      <button @click="startCountdown">开始倒计时</button>
    </div>
 
    <div>
      <div v-if="counting">
        第{{ currentCycle }}次循环 {{ formattedTime }}
      </div>
      <div v-else-if="finished">
        倒计时完成
      </div>
      <div v-else-if="error">
        输入错误
      </div>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue';
 
const countdownInterval = ref<NodeJS.Timeout | null>(null);
const cishu = ref<number>(5);
const timeone = ref<number>(3);
const currentCycle = ref<number>(1);
const counting = ref<boolean>(false);
const error = ref<boolean>(false);
const showForm = ref(true);
 
function startCountdown() {
  if (isNaN(cishu.value) || isNaN(timeone.value)) {
    error.value = true;
    return;
  }
 
  error.value = false;
  counting.value = true;
  showForm.value = false;
 
  let remainingSeconds = timeone.value * 60;
  const countdownFn = () => {
    if (remainingSeconds <= 0) {
      if (currentCycle.value < cishu.value) {
        currentCycle.value++;
        remainingSeconds = timeone.value * 60;
      } else {
        counting.value = false;
        finished.value = true;
        return;
      }
    }
 
    const minutes = Math.floor(remainingSeconds / 60);
    const seconds = remainingSeconds % 60;
 
    // 更新显示的时间
    formattedTime.value = `${minutes} 分, ${seconds.toString().padStart(2, '0')} 秒`;
 
    remainingSeconds--;
 
    // 每秒递减
    countdownInterval.value = setTimeout(countdownFn, 1000);
  };
 
  countdownFn();
 
  // 清除倒计时
  onMounted(() => {
    clearTimeout(countdownInterval.value!);
  });
}
 
// 格式化显示的时间
const formattedTime = ref<string>('00 分, 00 秒');
const finished = ref<boolean>(false);
 
watch([cishu, timeone], () => {
  counting.value = false;
  finished.value = false;
  error.value = false;
});
 
watch(finished, (newValue) => {
  if (newValue) {
    window.location.reload();
  }
});
</script>

 

相关推荐

  1. 基于vue3实现计时60s的

    2024-04-02 02:20:05       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-02 02:20:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-02 02:20:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-02 02:20:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-02 02:20:05       20 阅读

热门阅读

  1. 纯css 实现div 或者 图片一大一小的过渡动画

    2024-04-02 02:20:05       13 阅读
  2. LeetCode //C - 436. Find Right Interval

    2024-04-02 02:20:05       14 阅读
  3. Windows下配深度学习环境

    2024-04-02 02:20:05       15 阅读
  4. docker入门

    2024-04-02 02:20:05       15 阅读
  5. python中线程与协程

    2024-04-02 02:20:05       16 阅读
  6. 微信小程序中实现埋点的方法

    2024-04-02 02:20:05       17 阅读
  7. Azure入门实践-如何创建两个虚拟网络的对等连接

    2024-04-02 02:20:05       19 阅读
  8. C++ 学习10大网站推荐(Bjarne Stroustrup)

    2024-04-02 02:20:05       18 阅读
  9. 二分查找算法刷题记录 -LC34

    2024-04-02 02:20:05       13 阅读
  10. Linux 服务service(一)

    2024-04-02 02:20:05       17 阅读
  11. Nginx: proxy_set_header 与 add_header 区别

    2024-04-02 02:20:05       16 阅读