摘要:在微信公眾號(hào)開(kāi)發(fā)過(guò)程中要實(shí)現(xiàn)倒計(jì)時(shí)的一個(gè)功能。原因是解決方案訂單生成的時(shí)候我們記錄下這個(gè)時(shí)間為,時(shí)間間隔為分鐘內(nèi)需要付款,為,為現(xiàn)在的時(shí)間。那么倒計(jì)時(shí)時(shí)間,代碼如下支付超時(shí)訂單已提交,請(qǐng)?jiān)诜置雰?nèi)完成支付
在微信公眾號(hào)開(kāi)發(fā)過(guò)程中要實(shí)現(xiàn)倒計(jì)時(shí)的一個(gè)功能。效果如下:
開(kāi)始的思路沒(méi)有考慮頁(yè)面在后臺(tái)運(yùn)行以及鎖屏等情況。代碼如下:
let interval = setInterval(() => { let {staticTime} = this.state; staticTime = staticTime - 1; if (staticTime <= 0) { clearInterval(interval); this.setState({ tip:"支付超時(shí)", staticTime:0 }); return; } let minutes = parseInt(staticTime/60); let Seconds = staticTime%60; let tip = "訂單已提交,請(qǐng)?jiān)?+minutes+"分"+Seconds+"秒內(nèi)完成支付"; this.setState({ tip:tip, staticTime:staticTime }); }, 1000); 后來(lái)測(cè)試發(fā)現(xiàn)鎖屏或者把頁(yè)面留在后臺(tái),計(jì)算就不對(duì),于是把代碼進(jìn)行了如下改造。 let interval = setInterval(() => { let {backGroundTime, staticTime} = this.state; this.setState({ backGroundTime:0 }); staticTime = staticTime - backGroundTime - 1; if (staticTime <= 0) { clearInterval(interval); this.setState({ tip:"支付超時(shí)", staticTime:0, }); return; } let minutes = parseInt(staticTime/60); let Seconds = staticTime%60; let tip = "訂單已提交,請(qǐng)?jiān)?+minutes+"分"+Seconds+"秒內(nèi)完成支付"; this.setState({ tip:tip, staticTime:staticTime, }); }, 1000); this.listenPageShowHideHandle(); //計(jì)算頁(yè)面在后臺(tái)的時(shí)間
listenPageShowHideHandle = () =>{
let {backGroundTime} = this.state; let start, end; let self = this; document.addEventListener("visibilitychange", function() { if(document.visibilityState == "hidden"){ start = new Date().getTime(); }else if(document.visibilityState == "visible"){ end = new Date().getTime(); backGroundTime = Math.floor((end - start)/1000); self.setState({backGroundTime}); console.log("時(shí)間差:", backGroundTime); } console.log( document.visibilityState ); });
}
改造之后發(fā)先問(wèn)題依然存在。原因是: You cannot continue to run javascript while the iPhone is sleeping using setTimeout(), however.When the phone is put to sleep, Safari will kill any running javascript processes using setTimeout(). Check out this answer here for some reasons why this is done. **解決方案:** 訂單生成的時(shí)候我們記錄下這個(gè)時(shí)間為A, 時(shí)間間隔為B(3分鐘內(nèi)需要付款,B為3*60*1000),C為現(xiàn)在的時(shí)間。我們使用setInterval 每個(gè)1秒讀取一下時(shí)間。那么倒計(jì)時(shí)時(shí)間 == A+B-C,代碼如下 let interval = setInterval(()=>{ let {orderTime, staticTime} = this.state; let nowTime = Date.now(); let sub = Math.floor((orderTime + staticTime - nowTime)/1000); console.log("sub",sub); if(sub<=0){ clearInterval(interval); this.setState({ tip:"支付超時(shí)", isFalse:true }); return; } let minutes = parseInt(sub/60); let Seconds = sub%60; let tip = "訂單已提交,請(qǐng)?jiān)?+minutes+"分"+Seconds+"秒內(nèi)完成支付"; console.log(tip); this.setState({ tip:tip, isFalse:false }); },1000);
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/108126.html
摘要:微信小程序應(yīng)用號(hào)開(kāi)發(fā)資源匯總文檔工具教程代碼插件組件文檔從搭建一個(gè)微信小程序開(kāi)始小程序開(kāi)發(fā)文檔小程序設(shè)計(jì)指南工具小程序開(kāi)發(fā)者工具官方支持微信小程序?qū)崟r(shí)預(yù)覽的支持的微信小程序組件化開(kāi)發(fā)框架轉(zhuǎn)在線工具小程序云端增強(qiáng)社區(qū)微信小程序 微信(小程序or應(yīng)用號(hào))開(kāi)發(fā)資源匯總-文檔-工具-教程-代碼-插件-組件 文檔 從搭建一個(gè)微信小程序開(kāi)始 小程序開(kāi)發(fā)文檔 小程序設(shè)計(jì)指南 工具 小程序開(kāi)發(fā)者...
摘要:網(wǎng)上有很多前端的學(xué)習(xí)路徑文章,大多是知識(shí)點(diǎn)羅列為主或是資料的匯總,數(shù)據(jù)量讓新人望而卻步。天了解一個(gè)前端框架。也可以關(guān)注微信公眾號(hào)曉舟報(bào)告,發(fā)送獲取資料,就能收到下載密碼,網(wǎng)盤地址在最下方,獲取教程和案例的資料。 前言 好的學(xué)習(xí)方法可以事半功倍,好的學(xué)習(xí)路徑可以指明前進(jìn)方向。這篇文章不僅要寫(xiě)學(xué)習(xí)路徑,還要寫(xiě)學(xué)習(xí)方法,還要發(fā)資料,干貨滿滿,準(zhǔn)備接招。 網(wǎng)上有很多前端的學(xué)習(xí)路徑文章,大多是知...
摘要:微信小程序中的每一個(gè)頁(yè)面的路徑頁(yè)面名都需要寫(xiě)在的中,且中的第一個(gè)頁(yè)面是小程序的首頁(yè)。真機(jī)運(yùn)行截圖運(yùn)行于,微信版本功能能夠計(jì)算里程時(shí)間實(shí)時(shí)獲取跑步路徑有些粗糙思路主要使用了微信小程序的獲取位置和地圖組件。 首發(fā)地址 一、準(zhǔn)備工作 1、注冊(cè)一個(gè)小程序賬號(hào),得用一個(gè)沒(méi)注冊(cè)過(guò)公眾號(hào)的郵箱注冊(cè)。2、注冊(cè)過(guò)程中需要很多認(rèn)證,有很多認(rèn)證,比較繁瑣,如果暫時(shí)只是開(kāi)發(fā)測(cè)試,不進(jìn)行提審、發(fā)布的話,只要完成...
閱讀 1112·2023-04-26 01:47
閱讀 1762·2021-11-18 13:19
閱讀 2114·2019-08-30 15:44
閱讀 712·2019-08-30 15:44
閱讀 2410·2019-08-30 15:44
閱讀 1293·2019-08-30 14:06
閱讀 1476·2019-08-30 12:59
閱讀 1951·2019-08-29 12:49