導(dǎo)讀微信(WeChat)是騰訊公司于2011年1月21日推出的一個(gè)為智能終端提供即時(shí)通訊服務(wù)的免費(fèi)應(yīng)用程序,由張小龍所帶領(lǐng)的騰訊廣州研發(fā)中心產(chǎn)品團(tuán)隊(duì)打造 [2] 。微信支持跨通信運(yùn)營(yíng)商、跨操作系統(tǒng)平臺(tái)... 微信(WeChat)是騰訊公司于2011年1月21日推出的一個(gè)為智能終端提供即時(shí)通訊服務(wù)的免費(fèi)應(yīng)用程序,由張小龍所帶領(lǐng)的騰訊廣州研發(fā)中心產(chǎn)品團(tuán)隊(duì)打造 [2] 。微信支持跨通信運(yùn)營(yíng)商、跨操作系統(tǒng)平臺(tái)通過網(wǎng)絡(luò)快速發(fā)送免費(fèi)(需消耗少量網(wǎng)絡(luò)流量)語(yǔ)音短信、視頻、圖片和文字,同時(shí),也可以使用通過共享流媒體內(nèi)容的資料和基于位置的社交插件“搖一搖”、“漂流瓶”、“朋友圈”、”公眾平臺(tái)“、”語(yǔ)音記事本“等服務(wù)插件。 以下是我自己編寫的一個(gè)代碼,功能是在微信公眾號(hào)開發(fā)過程中實(shí)現(xiàn)倒計(jì)時(shí)的。效果如下,訂單已提交,請(qǐng)?jiān)?分57秒內(nè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)?#39;+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)?#39;+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ā)先問題依然存在。原因是: 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)?#39;+minutes+'分'+Seconds+'秒內(nèi)完成支付'; console.log(tip); this.setState({ tip:tip, isFalse:false }); },1000); apache php mysql 相關(guān)文章: 微信公眾號(hào)授權(quán)設(shè)置,微信公眾授權(quán) 微信公眾號(hào)點(diǎn)擊菜單即可打開并登錄微站的實(shí)現(xiàn)方法 相關(guān)視頻: 以上就是微信公眾號(hào)開發(fā),實(shí)現(xiàn)倒計(jì)時(shí)的一個(gè)功能(純代碼)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章! 微信提供公眾平臺(tái)、朋友圈、消息推送等功能,用戶可以通過“搖一搖”、“搜索號(hào)碼”、“附近的人”、掃二維碼方式添加好友和關(guān)注公眾平臺(tái),同時(shí)微信將內(nèi)容分享給好友以及將用戶看到的精彩內(nèi)容分享到微信朋友圈。 |
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!