vue實(shí)現(xiàn)同時(shí)設(shè)置多個(gè)倒計(jì)時(shí)
本文實(shí)例為大家分享了vue實(shí)現(xiàn)同時(shí)設(shè)置多個(gè)倒計(jì)時(shí)的具體代碼,供大家參考,具體內(nèi)容如下
html如下:
<div class="home">
<tbody>
<tr v-for="(item, index) in bargainGoods" :key="index">
<td v-text="item.down + Djs_timeList(item.countDown)"></td>
</tr>
</tbody>
</div>
js如下:
export default {
data() {
return {
bargainGoods: [],
total: 0,
page: 1,
serverTime: 0,
timer: ""
// hostUrl: this.$hostUrl
};
},
//用于數(shù)據(jù)初始化
created: function() {
// 獲取數(shù)據(jù)
this.goods();
// 獲取服務(wù)器時(shí)間
this.findServiceTime();
},
methods: {
goods: function() {
var _this = this;
_this.$axios({
url: "goods/pageGoods",
data: {
current: -1,
activityStatus: "",
limit: -1,
status: "SALE"
},
success: response => {
_this.bargainGoods = response.items;
_this.Djs_time();// 調(diào)用定時(shí)器
// 以下是我處理的后臺(tái)返回?cái)?shù)據(jù) 開(kāi)始時(shí)間和結(jié)束時(shí)間,頁(yè)面顯示用的
if (_this.bargainGoods.length != 0) {
for (var key in _this.bargainGoods) {
var hour = 0;
var startime = 0;
if (_this.bargainGoods[key] != null) {
_this.bargainGoods[key].countDown = "";
_this.bargainGoods[key].down = "";
// 結(jié)束時(shí)間
hour = _this.bargainGoods[key].overTime;
startime = _this.bargainGoods[key].activityStartTime;
hour = hour.replace(/-/g, "/");
hour = new Date(hour).getTime();
startime = startime.replace(/-/g, "/");
startime = new Date(startime).getTime();
// 如果結(jié)束時(shí)間大于當(dāng)前時(shí)間
if (hour > _this.serverTime && startime < _this.serverTime) {
var hourtime = hour - _this.serverTime;
if (hourtime > 0) {
_this.bargainGoods[key].down = "結(jié)束倒計(jì)時(shí):";
_this.bargainGoods[key].countDown =
_this.bargainGoods[key].overTime;
}
} else if (startime > _this.serverTime) {
var starhourtime = startime - _this.serverTime;
if (starhourtime > 0) {
_this.bargainGoods[key].down = "開(kāi)始倒計(jì)時(shí):";
_this.bargainGoods[key].countDown =
_this.bargainGoods[key].activityStartTime;
}
} else {
_this.bargainGoods[key].down = "已結(jié)束";
_this.bargainGoods[key].countDown = "";
}
// console.log(_this.bargainGoods);
}
}
_this.bargainGoods = _this.bargainGoods;
}
}
});
},
// 獲取服務(wù)器時(shí)間
findServiceTime() {
var _this = this;
_this.$axios({
url: "serverTime/getDateTime",
success: function(res) {
_this.serverTime = res.item;
}
});
},
Djs_time: function() {
this.timer = setInterval(() => {
this.serverTime = this.serverTime + 1000;
}, 1000);
},
Djs_timeList: function(itemEnd) {
// 此處 itemEnd 的日期是年月日時(shí)分秒
var endItem = new Date(itemEnd).getTime(); //獲取列表傳的截止時(shí)間,轉(zhuǎn)成時(shí)間戳
var nowItem = this.serverTime; //獲取當(dāng)前時(shí)間
var rightTime = endItem - nowItem; //截止時(shí)間減去當(dāng)前時(shí)間
if (rightTime > 0) {
//判斷剩余倒計(jì)時(shí)時(shí)間如果大于0就執(zhí)行倒計(jì)時(shí)否則就結(jié)束
var dd = Math.floor(rightTime / 1000 / 60 / 60 / 24);
var hh = Math.floor((rightTime / 1000 / 60 / 60) % 24);
var mm = Math.floor((rightTime / 1000 / 60) % 60);
var ss = Math.floor((rightTime / 1000) % 60);
var showTime = dd + "天" + hh + "小時(shí)" + mm + "分" + ss + "秒";
} else {
var showTime = "";
}
return showTime;
},
},
//離開(kāi)頁(yè)面后清除定時(shí)器
destroyed() {
clearInterval(this.timer);
}
};
效果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue3?實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能
- vue實(shí)現(xiàn)列表倒計(jì)時(shí)
- Vue3?實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(刷新保持狀態(tài))
- vue實(shí)現(xiàn)點(diǎn)擊按鈕倒計(jì)時(shí)
- Vue實(shí)現(xiàn)倒計(jì)時(shí)小功能
- vue實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)按鈕
- vue實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能
- Vue倒計(jì)時(shí)3秒后返回首頁(yè)Demo(推薦)
相關(guān)文章
Vue3狀態(tài)管理之Pinia的入門(mén)使用教程
Pinia是Vue.js的輕量級(jí)狀態(tài)管理庫(kù),比起vue3中的Vuex狀態(tài)管理,pinia更輕量,更容易使用,下面這篇文章主要給大家介紹了關(guān)于Vue3狀態(tài)管理之Pinia的入門(mén)使用教程,需要的朋友可以參考下2022-04-04
詳解三種方式解決vue中v-html元素中標(biāo)簽樣式
這篇文章主要介紹了三種方式解決vue中v-html元素中標(biāo)簽樣式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
vue實(shí)現(xiàn)滑塊拖拽校驗(yàn)功能的全過(guò)程
vue驗(yàn)證滑塊功能,在生活中很多地方都可以見(jiàn)到,使用起來(lái)非常方便,這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)滑塊拖拽校驗(yàn)功能的相關(guān)資料,需要的朋友可以參考下2021-08-08
vue中{{}},v-text和v-html區(qū)別與應(yīng)用詳解
這篇文章主要介紹了vue中{{}},v-text和v-html區(qū)別與應(yīng)用詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Vue使用Antd中a-table實(shí)現(xiàn)表格數(shù)據(jù)列合并展示示例代碼
文章介紹了如何在Vue中使用Ant?Design的a-table組件實(shí)現(xiàn)表格數(shù)據(jù)列合并展示,通過(guò)處理函數(shù)對(duì)源碼數(shù)據(jù)進(jìn)行操作,處理相同數(shù)據(jù)時(shí)合并列單元格2024-11-11
elementui中的el-cascader級(jí)聯(lián)選擇器的實(shí)踐
本文主要介紹了elementui中的el-cascader級(jí)聯(lián)選擇器的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
vue自定義穿梭框支持遠(yuǎn)程滾動(dòng)加載的實(shí)現(xiàn)方法
這篇文章主要介紹了vue自定義穿梭框支持遠(yuǎn)程滾動(dòng)加載,iview是全局注入,基本使用原先的類名進(jìn)行二次創(chuàng)建公共組件,修改基礎(chǔ)js實(shí)現(xiàn)邏輯,本文結(jié)合實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08

