The countdown cannot be stopped.

< 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:"-sharpapp",
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

Mar.02,2021

what you need clear is var id not getMyTime


this points to the problem,
clipboard.png

beforeDestroy () {} just use this directly. In general, use the arrow function to save the this and point to
above using this.id=setInter.
under clear (this.id)

.
Menu