Vue3+Ts中编写定时器

window.setInterval()的类型是number。

完整代码:

<script lang="ts">
import {
  defineComponent,
  reactive,
  onMounted,
} from "vue";

export default defineComponent({
  name: "index",
  setup() {
    const state = reactive({
      time: 60,  // 60s倒计时
      timer: 0,  // 定时器对象
    });
    onMounted(() => {
       // 60s倒计时
       state.timer = setInterval(() => {
         state.time--;
         if (!state.time) {
           state.time = 60;
           clearInterval(state.timer);
           state.timer = 0;
         }
       }, 1000);
    });
  },
});
</script>

你可能感兴趣的:(vue,vue.js,javascript,ecmascript)