這篇文章主要介紹vue如何實現同時設置多個倒計時,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體內容如下
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
};
},
//用于數據初始化
created: function() {
// 獲取數據
this.goods();
// 獲取服務器時間
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();// 調用定時器
// 以下是我處理的后臺返回數據 開始時間和結束時間,頁面顯示用的
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 = "";
// 結束時間
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();
// 如果結束時間大于當前時間
if (hour > _this.serverTime && startime < _this.serverTime) {
var hourtime = hour - _this.serverTime;
if (hourtime > 0) {
_this.bargainGoods[key].down = "結束倒計時:";
_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 = "開始倒計時:";
_this.bargainGoods[key].countDown =
_this.bargainGoods[key].activityStartTime;
}
} else {
_this.bargainGoods[key].down = "已結束";
_this.bargainGoods[key].countDown = "";
}
// console.log(_this.bargainGoods);
}
}
_this.bargainGoods = _this.bargainGoods;
}
}
});
},
// 獲取服務器時間
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 的日期是年月日時分秒
var endItem = new Date(itemEnd).getTime(); //獲取列表傳的截止時間,轉成時間戳
var nowItem = this.serverTime; //獲取當前時間
var rightTime = endItem - nowItem; //截止時間減去當前時間
if (rightTime > 0) {
//判斷剩余倒計時時間如果大于0就執行倒計時否則就結束
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 + "小時" + mm + "分" + ss + "秒";
} else {
var showTime = "";
}
return showTime;
},
},
//離開頁面后清除定時器
destroyed() {
clearInterval(this.timer);
}
};效果如下:

Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以創建可維護性和可測試性更強的代碼庫,Vue允許可以將一個網頁分割成可復用的組件,每個組件都包含屬于自己的HTML、CSS、JavaScript,以用來渲染網頁中相應的地方,所以越來越多的前端開發者使用vue。
以上是“vue如何實現同時設置多個倒計時”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。