


WeChat public account development to implement a countdown function (pure code)
Jul 27, 2018 pm 04:35 PMThe following is a code I wrote myself. The function is to implement countdown during the development process of WeChat public account. The effect is as follows, the order has been submitted, please complete the payment within 2 minutes and 57 seconds. Pure code analysis.
#The initial idea did not consider the situation of the page running in the background and locking the screen. The code is as follows:
let interval = setInterval(() => { let {staticTime} = this.state; staticTime = staticTime - 1; if (staticTime <= 0) { clearInterval(interval); this.setState({ tip:'支付超時', staticTime:0 }); return; } let minutes = parseInt(staticTime/60); let Seconds = staticTime%60; let tip = '訂單已提交,請在'+minutes+'分'+Seconds+'秒內(nèi)完成支付'; this.setState({ tip:tip, staticTime:staticTime }); }, 1000); 后來測試發(fā)現(xià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:'支付超時', staticTime:0, }); return; } let minutes = parseInt(staticTime/60); let Seconds = staticTime%60; let tip = '訂單已提交,請在'+minutes+'分'+Seconds+'秒內(nèi)完成支付'; this.setState({ tip:tip, staticTime:staticTime, }); }, 1000); this.listenPageShowHideHandle(); //計算頁面在后臺的時間
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('時間差:', 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. **解決方案:** 訂單生成的時候我們記錄下這個時間為A, 時間間隔為B(3分鐘內(nèi)需要付款,B為3*60*1000),C為現(xiàn)在的時間。我們使用setInterval 每個1秒讀取一下時間。那么倒計時時間 == 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:'支付超時', isFalse:true }); return; } let minutes = parseInt(sub/60); let Seconds = sub%60; let tip = '訂單已提交,請在'+minutes+'分'+Seconds+'秒內(nèi)完成支付'; console.log(tip); this.setState({ tip:tip, isFalse:false }); },1000);
apache php mysql
Related articles:
WeChat public account authorization settings, WeChat public authorization
WeChat public account clicks the menu to open and log in to the microsite implementation method
Related Video:
Chuanzhi and Dark Horse WeChat public platform development video tutorial
The above is the detailed content of WeChat public account development to implement a countdown function (pure code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The OEX platform is a highly-watched digital asset trading platform, providing users with a safe, convenient and efficient trading experience. Here, you can conveniently buy, sell and trade various mainstream digital currencies such as Bitcoin and Ethereum, and enjoy the rich financial derivative services provided by the platform.

The official Ethereum app does not exist, iOS users can manage ETH through cryptocurrency wallets or exchange apps. Recommended mainstream apps include: 1. Binance, powerful and integrated Web3 wallet, suitable for trading and DeFi; 2. OkX, with smooth experience and Web3 integration, supporting DeFi and NFT; 3. MetaMask, as the core non-custodial wallet, is a necessary tool for connecting DeFi and NFT platforms; 4. Huobi (HTX), provides rich trading pairs and financial derivatives, suitable for old users; 5. Trust Wallet, simple interface, suitable for new non-custodial wallets; 6. Gate.io, suitable for exploring emerging currencies

iPhone does not support creating contact groups directly in the address book application, but similar functions can be achieved through the following methods: 1. Use the "Group" function in the address book application, click "Edit" > "Add Group" to create a custom group; 2. Add each contact to the corresponding group by editing it; 3. If you need to send a group message, you can create a new multi-person conversation in the information application; 4. Use iCloud or Gmail to synchronize tagged groups to facilitate the management of a large number of contacts on the computer. These methods are used in conjunction with efficient management of contact groups.

TocheckifaniPhoneisunlocked,1)gotoSettings>Cellular>CellularDataOptionsandlookforlabelslike“Unlocked”or“NoSIMrestrictions,”2)insertadifferentcarrier’sactiveSIMcardandseeifthephoneacceptsit,3)usetheIMEInumberononlinetoolslikeIMEI.infotocheckunlo

Determining the depreciation value of iPhone requires multiple factors and comparing different platforms. 2. The first choice is Apple's official replacement plan, which has a simple and transparent process, but the final valuation must be based on the actual condition of the equipment. 3. Third-party websites such as Gazelle, Swappa, etc. may provide higher quotations, but you need to pay attention to describing the real situation and avoiding the charging trap. 4. The core factors affecting value include model and storage capacity, equipment status, operator lock status, etc. Small problems may also affect the quotation. 5. If you pursue higher returns, you can choose to sell privately, but you need to weigh the time cost and transaction risks.

To create PersonalVoice in iOS17, you first need to enable the feature in the settings and record a sound model for about five minutes. The specific steps are as follows: 1. Open "Settings" > "Assistance Functions" > "Narration", click "PersonalVoice" and select "Create PersonalVoice"; 2. Read the text provided by the system in a quiet environment to keep the speech speed stable; 3. After creation, you can use your personalized voice to read messages, notifications, etc. in "Narration", or switch back to the default voice; 4. Currently, only English and some languages ??are supported, and Chinese is not supported at the moment; 5. Pay attention to environmental noise, intonation consistency and storage space when recording, and re-record unsatisfactory content. Voice mode

Migrating contacts can be accomplished in three ways when replacing a new iPhone: First, using iCloud synchronization is the most worry-free, ensuring that the old phone has turned on the iCloud address book backup, and the new phone logs into the same account and enables synchronization; Second, using AirDrop is suitable for a small number of contacts to transmit. After setting up AirDrop visibility on both phones, sending and receiving them through the share button; Third, with the help of computer relay, both Mac or Windows, synchronizing contacts through Finder or iTunes and replacing or merging data. Select the method that suits you and pay attention to your account and permission settings to easily complete the migration.

Purchase USDT and other stablecoins on Apple phones, you must download the compliant exchange app through the overseas Apple ID and complete the authentication and conduct transactions. The specific steps are as follows: 1. Use Apple ID in the non-mainland region to log in to the App Store; 2. Download recommended platforms such as OKX, Binance, Huobi (HTX), Gate.io or KuCoin; 3. Complete registration and identity authentication (KYC); 4. Enter the "Buy Coin" or "C2C/P2P Transaction" page to select USDT; 5. Filter merchants according to the payment method and place orders; 6. Pay through Alipay, WeChat or bank card and confirm transactions; 7. Wait for the merchant to put the coins into the account, and the entire process is completed.
