cankao-h5/src/stores/timerStore.ts

33 lines
669 B
TypeScript

import { defineStore } from "pinia";
interface ITimerState {
timerId: any;
isTimerRunning: boolean;
}
export const useTimerStore = defineStore("timer", {
state: (): ITimerState => ({
timerId: null,
isTimerRunning: false,
}),
actions: {
startTimer(cb: Function, interval: number) {
if (!this.isTimerRunning) {
this.timerId = setInterval(cb, interval);
this.isTimerRunning = true;
}
},
stopTimer() {
if (this.isTimerRunning) {
clearInterval(this.timerId);
this.timerId = null;
this.isTimerRunning = false;
}
},
resetTimer() {
this.stopTimer();
},
},
});