鍍金池/ 問答/HTML/ 倒計時無法停止

倒計時無法停止

<div id="app">

<div class="main">
    <div  v-for="item in items"  >
        {{item.day}}天{{item.hour}}時{{item.minute}}分{{item.second}}秒
    </div>
</div>

</div>
<scrip>
var app = new Vue({

el:'#app',
data:{
        items: [
            {day: '   ', hour: '   ', minute: '   ', second: '   '}
        ]
},
created:function(){
    this.getMyTime();
    this.start();
},
methods: {
    format: function (n) {
        return n < 10 ? '0' + n : n;
    },
    getMyTime: function () {
        var startDate = new Date();
        var endDate = new Date('2018/3/30 11:10:00');
        var countDown = parseInt(endDate.getTime() - startDate.getTime()) / 1000;
        var day = parseInt(countDown / (24 * 60 * 60));
        var h = parseInt(countDown / (60 * 60) % 24);
        var m = parseInt(countDown / 60 % 60);
        var s = parseInt(countDown % 60);
        this.items[0].day = day;
        this.items[0].hour = this.format(h);
        this.items[0].minute = this.format(m);
        this.items[0].second = this.format(s);
    },
    start: function () {
        var that = this;
        var id = setInterval(function () {
            that.getMyTime();
        }, 500);
    beforeDestroy: function () {
        if (countDown <= 0) {
            if (that.getMyTime()) {
                clearInterval(that.getMyTime());
                // console.log("投票結束!");
            }
        }
    }
}

})
</scrip>

clipboard.png

回答
編輯回答
念初

this指向問題啊,
clipboard.png

beforeDestroy(){}直接用this就好了,一般的地方都用箭頭函數,省的來回找this指向
上面使用this.id=setInter...
下邊clear(this.id)

2017年5月19日 03:16
編輯回答
挽青絲

你需要 clear 的是 var id 不是 getMyTime

2017年7月13日 17:05