vue3没有this,取而代之的是proxy
所以在定时器的写法上也有所不同
话不多说直接上代码
<template> <p>剩余时间:{{ count }}</p> </template> export default{ const seconds = ref(1800);//定义一下秒数,根据秒数来计算,需要自己判断 const countDown = () => { let d = parseInt(seconds.value / (24 * 60 * 60)); d = d < 10 ? "0" + d : d; let h = parseInt((seconds.value / (60 * 60)) % 24); h = h < 10 ? "0" + h : h; let m = parseInt((seconds.value / 60) % 60); m = m < 10 ? "0" + m : m; let s = parseInt(seconds.value % 60); s = s < 10 ? "0" + s : s; count.value = m + "分" + s + "秒"; // console.log('监听',count.value ); }; // const timed = () => { dsq.value = setInterval(() => { seconds.value -= 1; countDown(); }, 1000); }; }
这样下来,一个倒计时30分钟的定时器就写好了