vue頁面使用多個(gè)定時(shí)器的方法
本文實(shí)例為大家分享了vue頁面使用多個(gè)定時(shí)器的具體代碼,供大家參考,具體內(nèi)容如下
問題描述
vue頁面使用多個(gè)定時(shí)器
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:請(qǐng)求數(shù)據(jù),遍歷數(shù)組,然后根據(jù)數(shù)據(jù)字段判斷,如果服務(wù)器的開始時(shí)間小于服務(wù)器的系統(tǒng)時(shí)間那就倒計(jì)時(shí),appointmentStatus
這個(gè)字段為2的時(shí)候 且服務(wù)器的開始時(shí)間小于服務(wù)器的系統(tǒng)時(shí)間.let appointStart = new Date( item.appointStart.replace(/-/g, "/") ).getTime();
這個(gè)是兼容ios的時(shí)間格式
const getProdList = async () => { ? ? ? //預(yù)售商品列表 ? ? ? await $api.fns.PreSale.getPreSaleList({ ? ? ? ? params: { ? ? ? ? ? iconType: 2, ? ? ? ? }, ? ? ? }) ? ? ? ? .then((res) => { ? ? ? ? ? if (res?.data) { ? ? ? ? ? ? console.log(res.data); ? ? ? ? ? ? // `appointment_status`'預(yù)約狀態(tài) 1已上架、2已下架', ? ? ? ? ? ? state.list = res.data; ? ? ? ? ? ? res.data.map((item) => { ? ? ? ? ? ? ? item.isDisabled = true; ? ? ? ? ? ? ? item.saleTit = "等待預(yù)約"; ? ? ? ? ? ? ? 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) { ? ? ? ? ? ? ? ? ? // 還沒有開始預(yù)購 ? ? ? ? ? ? ? ? ? if (item.times > 0) { ? ? ? ? ? ? ? ? ? ? startCountdown(item.times, index); ? ? ? ? ? ? ? ? ? ? // 預(yù)購結(jié)束 ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? item.isDisabled = true; ? ? ? ? ? ? ? ? ? ? item.saleTit = "預(yù)購結(jié)束"; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? // 時(shí)間為空的時(shí)候,就只有預(yù)購結(jié)束,和立即預(yù)購兩種狀態(tài) ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? item.isDisabled = true; ? ? ? ? ? ? ? ? ? item.saleTit = "預(yù)購結(jié)束"; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? item.isDisabled = false; ? ? ? ? ? ? ? ? item.saleTit = "立即預(yù)購"; ? ? ? ? ? ? ? } ? ? ? ? ? ? }); ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? }) ? ? ? ? .catch((error) => { ? ? ? ? ? console.log(error); ? ? ? ? }); ? ? };
因?yàn)槊恳粋€(gè)定時(shí)器的時(shí)間都不一樣,所以每一個(gè)定時(shí)器結(jié)束了要清除定時(shí)器 window.clearInterval(state.list[index].timer);
// 倒計(jì)時(shí) const startCountdown = (times, index) => { ? ? ? console.log("index", index, state.list); ? ? ? // 跟開始時(shí)間相比如果當(dāng)前時(shí)間小于開始時(shí)間,那就還沒有開始 ? ? ? // 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 = "立即預(yù)購"; ? ? ? ? ? ? }); ? ? ? ? ? ? window.clearInterval(state.list[index].timer); ? ? ? ? ? } ? ? ? ? }, 1000); ? ? ? } ? ? };
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中的echarts圖表如何實(shí)現(xiàn)loading效果
這篇文章主要介紹了Vue中的echarts圖表如何實(shí)現(xiàn)loading效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue搜索高亮popsearch組件的實(shí)現(xiàn)示例
有時(shí)候給頁面內(nèi)容添加一個(gè)關(guān)鍵詞搜索功能,如果搜索結(jié)果能夠像瀏覽器搜索一樣高亮顯示,那找起來就會(huì)很明顯體驗(yàn)會(huì)好很多,本文就來介紹一下vue搜索高亮popsearch組件的實(shí)現(xiàn)示例,感興趣的可以了解一下2023-09-09vue中el-date-picker選擇日期的限制的項(xiàng)目實(shí)踐
ElementUI的el-date-picker使用時(shí),有時(shí)候想要限制用戶選擇的時(shí)間范圍,本文就來介紹了vue中el-date-picker選擇日期的限制的項(xiàng)目實(shí)踐,感興趣的可以了解一下2023-10-10Vue 重置data的數(shù)據(jù)為初始狀態(tài)操作
這篇文章主要介紹了Vue 重置data的數(shù)據(jù)為初始狀態(tài)操作方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03使用vue-router在Vue頁面之間傳遞數(shù)據(jù)的方法
這篇文章主要介紹了使用vue-router在Vue頁面之間傳遞數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07element-ui中Table表格省市區(qū)合并單元格的方法實(shí)現(xiàn)
這篇文章主要介紹了element-ui中Table表格省市區(qū)合并單元格的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vue初嘗試--項(xiàng)目結(jié)構(gòu)(推薦)
這篇文章主要介紹了vue初嘗試--項(xiàng)目結(jié)構(gòu)的相關(guān)知識(shí),需要的朋友可以參考下2018-01-01