Vue中倒計時的作法

項目須要作一個考試倒計時的功能,參考了CSDN上的一遍博文,作出本身的功能,在此謝謝sinat_17775997
```
this

export default {
data() {
return {
keepTime: '倒計時',
limittime:100,
settime: '',
flag: false
}
},
methods: {
StartCountDown() {
var mydate = new Date();
mydate.setMinutes(mydate.getMinutes() + this.limittime);
this.settime=mydate;.net

let time = setInterval(() => {
            if (this.flag == true) {
                clearInterval(time)
            }
            this.timeDown()
        }, 100)
    },
    timeDown() {
        const endTime = new Date(this.settime);
        const nowTime = new Date();
        let leftTime = parseInt((endTime.getTime() - nowTime.getTime()) / 1000);
        let d = parseInt(leftTime / (24 * 60 * 60));
        let h = this.formate(parseInt(leftTime / (60 * 60) % 24));
        let m = this.formate(parseInt(leftTime / 60 % 60));
        let s = this.formate(parseInt(leftTime % 60));
        if (leftTime <= 0) {
            this.flag = true;
            alert("時間到,中止做答");
        }
        this.keepTime = `${h}:${m}:${s}`;
    },
    formate(time) {
        if (time >= 10) {
            return time
        } else {
            return `0${time}`
        }
    },

}code

相關文章
相關標籤/搜索