vue頁面使用多個定時器的方法
本文實例為大家分享了vue頁面使用多個定時器的具體代碼,供大家參考,具體內容如下
問題描述
vue頁面使用多個定時器
html:
<div class="prod-item"> ? ? ? <ul> ? ? ? ? <li ? ? ? ? ? v-for="(item, index) in state.list" ? ? ? ? ? :key="index" ? ? ? ? ? :class="[ ? ? ? ? ? ? item.isDisabled ? 'active_li_02' : 'active_li_01', ? ? ? ? ? ? 'flex ml-10 mr-10 ?mt-25', ? ? ? ? ? ]" ? ? ? ? > ? ? ? ? ? <div class="img"> ? ? ? ? ? ? <img :src="item.imageUrl" alt="" /> ? ? ? ? ? </div> ? ? ? ? ? <div class="item-right"> ? ? ? ? ? ? <div ? ? ? ? ? ? ? :class="[ ? ? ? ? ? ? ? ? item.isDisabled ? 'active_title_02' : 'active_title_01', ? ? ? ? ? ? ? ? 'title', ? ? ? ? ? ? ? ]" ? ? ? ? ? ? > ? ? ? ? ? ? ? {{ item.name }} ? ? ? ? ? ? </div> ? ? ? ? ? ? <van-button ? ? ? ? ? ? ? type="default" ? ? ? ? ? ? ? :class="[ ? ? ? ? ? ? ? ? item.isDisabled ? 'active_btn_02' : 'active_btn_01', ? ? ? ? ? ? ? ? 'btn mt-30', ? ? ? ? ? ? ? ]" ? ? ? ? ? ? ? @click.stop="checkLoginUser(item)" ? ? ? ? ? ? ? :disabled="item.isDisabled" ? ? ? ? ? ? > ? ? ? ? ? ? ? {{ item.saleTit }} ? ? ? ? ? ? </van-button> ? ? ? ? ? </div> ? ? ? ? </li> ? ? ? </ul> </div>
js:
js:請求數據,遍歷數組,然后根據數據字段判斷,如果服務器的開始時間小于服務器的系統(tǒng)時間那就倒計時,appointmentStatus
這個字段為2的時候 且服務器的開始時間小于服務器的系統(tǒng)時間.let appointStart = new Date( item.appointStart.replace(/-/g, "/") ).getTime();
這個是兼容ios的時間格式
const getProdList = async () => { ? ? ? //預售商品列表 ? ? ? await $api.fns.PreSale.getPreSaleList({ ? ? ? ? params: { ? ? ? ? ? iconType: 2, ? ? ? ? }, ? ? ? }) ? ? ? ? .then((res) => { ? ? ? ? ? if (res?.data) { ? ? ? ? ? ? console.log(res.data); ? ? ? ? ? ? // `appointment_status`'預約狀態(tài) 1已上架、2已下架', ? ? ? ? ? ? state.list = res.data; ? ? ? ? ? ? res.data.map((item) => { ? ? ? ? ? ? ? item.isDisabled = true; ? ? ? ? ? ? ? item.saleTit = "等待預約"; ? ? ? ? ? ? ? item.timer = null; ? ? ? ? ? ? ? if (item.appointStart) { ? ? ? ? ? ? ? ? let appointStart = new Date( ? ? ? ? ? ? ? ? ? item.appointStart.replace(/-/g, "/") ? ? ? ? ? ? ? ? ).getTime(); ? ? ? ? ? ? ? ? let systemDate = new Date( ? ? ? ? ? ? ? ? ? item.systemDate.replace(/-/g, "/") ? ? ? ? ? ? ? ? ).getTime(); ? ? ? ? ? ? ? ? item.times = Math.round( ? ? ? ? ? ? ? ? ? parseInt(appointStart - systemDate) / 1000 ? ? ? ? ? ? ? ? ); ? ? ? ? ? ? ? } ? ? ? ? ? ? }); ? ? ? ? ? ? state.list = res.data; ? ? ? ? ? ? state.list.map((item, index) => { ? ? ? ? ? ? ? if (item.appointmentStatus === 2) { ? ? ? ? ? ? ? ? if (item.times) { ? ? ? ? ? ? ? ? ? // 還沒有開始預購 ? ? ? ? ? ? ? ? ? if (item.times > 0) { ? ? ? ? ? ? ? ? ? ? startCountdown(item.times, index); ? ? ? ? ? ? ? ? ? ? // 預購結束 ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? item.isDisabled = true; ? ? ? ? ? ? ? ? ? ? item.saleTit = "預購結束"; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? // 時間為空的時候,就只有預購結束,和立即預購兩種狀態(tài) ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? item.isDisabled = true; ? ? ? ? ? ? ? ? ? item.saleTit = "預購結束"; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? item.isDisabled = false; ? ? ? ? ? ? ? ? item.saleTit = "立即預購"; ? ? ? ? ? ? ? } ? ? ? ? ? ? }); ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? }) ? ? ? ? .catch((error) => { ? ? ? ? ? console.log(error); ? ? ? ? }); ? ? };
因為每一個定時器的時間都不一樣,所以每一個定時器結束了要清除定時器 window.clearInterval(state.list[index].timer);
// 倒計時 const startCountdown = (times, index) => { ? ? ? console.log("index", index, state.list); ? ? ? // 跟開始時間相比如果當前時間小于開始時間,那就還沒有開始 ? ? ? // let times = Math.round(parseInt(appointStart - systemDate) / 1000); ? ? ? if (times > 0) { ? ? ? ? state.list[index].timer = setInterval(() => { ? ? ? ? ? state.list[index].times--; ? ? ? ? ? console.log("state.times-----111", state.list[index].times); ? ? ? ? ? if (state.list[index].times === 0) { ? ? ? ? ? ? state.list[index].times = 0; ? ? ? ? ? ? state.list.map(() => { ? ? ? ? ? ? ? state.list[index].isDisabled = false; ? ? ? ? ? ? ? state.list[index].saleTit = "立即預購"; ? ? ? ? ? ? }); ? ? ? ? ? ? window.clearInterval(state.list[index].timer); ? ? ? ? ? } ? ? ? ? }, 1000); ? ? ? } ? ? };
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue中el-date-picker選擇日期的限制的項目實踐
ElementUI的el-date-picker使用時,有時候想要限制用戶選擇的時間范圍,本文就來介紹了vue中el-date-picker選擇日期的限制的項目實踐,感興趣的可以了解一下2023-10-10element-ui中Table表格省市區(qū)合并單元格的方法實現
這篇文章主要介紹了element-ui中Table表格省市區(qū)合并單元格的方法實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08