vue3 <script setup> 语法糖时间组件

html 复制代码
<template>
  <div>
    <p>当前时间Current Time: {{ currentTime }}</p>
  </div>
</template>

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

const currentTime = ref('')

let interval // 声明 interval 变量

const getToday = () => {
  const datas = new Date()
  const on2 = ' : '
  let onS = datas.getHours()
  let onF = datas.getMinutes()
  let onN = datas.getSeconds()

  if (onS >= 0 && onS <= 9) {
    onS = '0' + onS
  }
  if (onF >= 0 && onF <= 9) {
    onF = '0' + onF
  }
  if (onN >= 0 && onN <= 9) {
    onN = '0' + onN
  }

  currentTime.value = onS + on2 + onF + on2 + onN
}

onMounted(() => {
  interval = setInterval(() => {
    // 将 interval 赋值为 setInterval 的返回值
    getToday()
  }, 1000)
})

onBeforeUnmount(() => {
  clearInterval(interval) // 使用 clearInterval 清除 interval 计时器
})
</script>
<style scoped>
p {
  font-size: 30px;
  color: hsla(160, 100%, 37%, 1);
    text-shadow: 1px 1px 1px rgb(0, 0, 0);
}
</style>
相关推荐
We་ct6 分钟前
LeetCode 205. 同构字符串:解题思路+代码优化全解析
前端·算法·leetcode·typescript
2301_8127314141 分钟前
CSS3笔记
前端·笔记·css3
ziblog44 分钟前
CSS3白云飘动动画特效
前端·css·css3
越努力越幸运50844 分钟前
CSS3学习之网格布局grid
前端·学习·css3
半斤鸡胗1 小时前
css3基础
前端·css
ziblog1 小时前
CSS3创意精美页面过渡动画效果
前端·css·css3
akangznl1 小时前
第四章 初识css3
前端·css·css3·html5
会豪1 小时前
深入理解 CSS3 滤镜(filter):从基础到实战进阶
前端·css·css3
头顶一只喵喵1 小时前
CSS3进阶知识:CSS3盒子模型,box-sizing:content-box和box-sizing:border-box的讲解
前端·css·css3
小飞大王6661 小时前
css进阶用法
前端·css