vue倒計時(超過兩小時不調(diào)用)

1掀泳、父組件countdownView.vue

<template>
    <div>
        <!-- 2 時(小時)=7200000 毫秒 -->
        <CountdownSon v-if="difference < 7200000 || difference == 7200000" :difference="difference"></CountdownSon>
    </div>
</template>

<script>
import CountdownSon from "./countdownSon";
export default {
    name: "countdownView",
    data () {
        return {
            startTime: '2023-04-01 08:00:00',
            endTime: '2023-04-01 09:00:00',
            difference: 0,
        }
    },
    components: {
        CountdownSon,
    },
    created () {
        this.gitInit()
    },
    methods: {
        gitInit () {
            const start = new Date(this.startTime);
            const end = new Date(this.endTime);
            // 計算時間差(毫秒)
            this.difference = end.getTime() - start.getTime();
            // 將時間差轉(zhuǎn)換為小時
            // this.timeDifference = difference / (1000 * 60 * 60)
        }
    }

};
</script>

1.1 父組件為遍歷出來的列表

<template>
    <div>
        <!-- 2 時(小時)=7200000 毫秒 -->
        <!-- <CountdownSon v-if="difference < 7200000 || difference == 7200000" :difference="difference"></CountdownSon> -->
        <div class="card" v-for="(item) in list" :key="item.id">
            <CountdownSon :difference="timeDifference(item.startTime, item.endTime)"></CountdownSon>
            <p>{{ timeDifference(item.startTime, item.endTime) }}</p>
            <p>{{ item.name }}</p>
        </div>
    </div>
</template>

<script>
import CountdownSon from "./countdownSon";
export default {
    name: "countdownView",
    data () {
        return {
            // startTime: '2023-04-01 08:00:00',
            // endTime: '2023-04-01 09:00:00',
            difference: 0,
            list: [
                { id: 1, name: "張三", startTime: '2023-04-01 08:00:00', endTime: '2023-04-01 08:10:00' },
                { id: 2, name: "李四", startTime: '2023-04-01 08:00:00', endTime: '2023-04-01 09:00:00' },
                { id: 3, name: "王五", startTime: '2023-04-01 08:00:00', endTime: '2023-04-01 09:30:00' }
            ]
        }
    },
    components: {
        CountdownSon,
    },
    created () {
        // this.gitInit()
    },
    methods: {
        // gitInit () {
        //     // 從接口獲取數(shù)據(jù)并計算時間差 以time1/time2為示例
        //     const start = new Date(this.startTime);
        //     const end = new Date(this.endTime);
        //     // 計算時間差(毫秒)
        //     this.difference = end.getTime() - start.getTime();
        //     // 將時間差轉(zhuǎn)換為小時
        //     // this.timeDifference = difference / (1000 * 60 * 60)
        // },
        timeDifference (start, end) {
            start = new Date(start);
            end = new Date(end);
            // 計算時間差(毫秒)
            return end.getTime() - start.getTime();
        }
    }

};
</script>
<style>
.card {
    width: 200px;
    height: 200px;
    border: 1px solid red;
}
</style>

2.子組件countdownSon.vue

<template>
    <div>
        倒計時:{{ displayTime }}
    </div>
</template>

<script>
export default {
    name: 'countdownSon',
    data () {
        return {
            displayTime: null,
            remainingTime: 0, // 剩余的倒計時時間绿满,單位為毫秒
            timer: null // 存儲定時器的引用
        };
    },
    props: {
        difference: {
            type: Number,
        },
    },
    created () {
        // 設(shè)置初始的倒計時時間為2小時
        // this.remainingTime = 2 * 60 * 60 * 1000; // 2小時 x 60分鐘 x 60秒 x 1000毫秒
        this.remainingTime = this.difference
        this.displayTime = this.getTime(this.remainingTime)
        this.startCountdown();
    },
    methods: {
        startCountdown () {
            // 設(shè)置定時器每秒更新倒計時
            this.timer = setInterval(() => {
                if (this.remainingTime > 0) {
                    this.remainingTime -= 1000; // 減少1秒
                    this.displayTime = this.getTime(this.remainingTime)
                } else {
                    this.stopCountdown(); // 倒計時結(jié)束
                }
            }, 1000);
        },
        stopCountdown () {
            // 清除定時器并且將其置為null
            clearInterval(this.timer);
            this.timer = null;
        },
        getTime (time) {
            const hours = Math.floor(time / 3600000);
            const minutes = Math.floor((time % 3600000) / 60000);
            const seconds = Math.floor((time % 60000) / 1000);
            return `${hours}小時${minutes}分${seconds}秒`
            // 將毫秒轉(zhuǎn)為時分秒
        }
    },
    beforeDestroy () {
        // 清除定時器防止內(nèi)存泄漏
        this.stopCountdown();
    }
};
</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末劲蜻,一起剝皮案震驚了整個濱河市喉誊,隨后出現(xiàn)的幾起案子帝簇,更是在濱河造成了極大的恐慌看靠,老刑警劉巖端朵,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異承疲,居然都是意外死亡邻耕,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門燕鸽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來兄世,“玉大人,你說我怎么就攤上這事啊研∮玻” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵党远,是天一觀的道長削解。 經(jīng)常有香客問我,道長沟娱,這世上最難降的妖魔是什么氛驮? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮济似,結(jié)果婚禮上矫废,老公的妹妹穿的比我還像新娘盏缤。我一直安慰自己,他們只是感情好磷脯,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布蛾找。 她就那樣靜靜地躺著,像睡著了一般赵誓。 火紅的嫁衣襯著肌膚如雪打毛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天俩功,我揣著相機與錄音幻枉,去河邊找鬼。 笑死诡蜓,一個胖子當著我的面吹牛熬甫,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蔓罚,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼椿肩,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了豺谈?” 一聲冷哼從身側(cè)響起郑象,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎茬末,沒想到半個月后厂榛,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡丽惭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年击奶,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片责掏。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡柜砾,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出换衬,到底是詐尸還是另有隱情局义,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布冗疮,位于F島的核電站,受9級特大地震影響檩帐,放射性物質(zhì)發(fā)生泄漏术幔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一湃密、第九天 我趴在偏房一處隱蔽的房頂上張望诅挑。 院中可真熱鬧四敞,春花似錦、人聲如沸拔妥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽没龙。三九已至铺厨,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間硬纤,已是汗流浹背解滓。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留筝家,地道東北人洼裤。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像溪王,于是被迫代替她去往敵國和親腮鞍。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容