Home Web Front-end H5 Tutorial How to use WeChat's embedded H5 web page to solve the problem of JS countdown failure

How to use WeChat's embedded H5 web page to solve the problem of JS countdown failure

Jun 11, 2018 pm 04:51 PM
h5 WeChat

Recently, with reference to project development, I encountered a need to embed the H5 mall page into the company's WeChat official account. During the development, I encountered a thorny problem, the js countdown failure problem. What's going on? Let’s talk about this problem that I solved by embedding h5 in WeChat. Friends who need it can refer to it

Project requirements: Nest the H5 mall page into the company’s WeChat public account

The project itself There is not much difference between the development of mobile web pages, but I encountered a problem yesterday. It is difficult to say, but it is easy to get there.

After the user places an order, there is a countdown logic on the payment method selection page (calculated from the time the order is placed, if the order is not paid after 24 hours, ws will automatically cancel the order), the js code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<script type="text/javascript"><br> var timespan = &#39;20160113&#39;; //后台程序生成24小时时间差值,这里随便写写

  var timer;

  function UpdateTime() {

   if (timespan > 0) {

    var hour = Math.floor(timespan / (60 * 60));

    var minute = Math.floor((timespan - (hour * 60 * 60)) / 60);

    var second = (timespan - (hour * 60 * 60) - (minute * 60));

    var word = "支付剩余时间" + (hour<10?("0"+hour):hour) + ":" + (minute<10?("0"+minute):minute) + ":" + (second<10?("0"+second):second) + ",逾期订单将自动取消~";

    timespan--;

    jQuery(".tc").html("<i class=&#39;time&#39;></i>" + word);

    timer = setTimeout("UpdateTime()", 1000);

   }

   else {

    $(&#39;.content&#39;).find(&#39;.w_op&#39;).hide();

    clearTimeout(timer);

    jQuery(".tc").html("<i class=&#39;time&#39;></i>" + "订单过期,已自动取消~");

    window.location.href=&#39;@Url.Action("orderDetail", "Order", new { OrderNumber = Model.No })&#39;;; //订单过期,跳转到订单详情页

   }

  }

  jQuery(document).ready(function () {

   UpdateTime();

  });<br></script>

Copy after login

The page effect is as follows:

Written in this way, there is no problem, and the local test is fine OK.

However, when it is placed outside, problems arise. In the Android version of WeChat, it will appear that when the phone locks the screen, the countdown does not run. It is still the time before turning off the screen. That is to say, during the time when the screen is turned off, time stops...

Later, of course it cannot be done later. I was anxious, all parties asked for help and looked for a way, so I came up with the following:

To implement the countdown, when entering the page for the first time, request the server to obtain the remaining time of the countdown, and then countdown again through js and other means on the page. ; After the phone locks the screen and then unlocks it, the countdown is still going on but the countdown time is inaccurate. The reason is that after the phone is unlocked, it does not request the server time to count down again, but the countdown is based on the last time the screen was locked. , this is because the browser has a cache. Without refreshing the page, the browser will cache the content requested for the first time. After the server is updated, the browser still displays the content for the first time.

Later, it was verified by multiple parties , can be set in meta, set page nocache, each time you visit the page, you need to re-obtain it from the server, instead of reading from the cache

expires sets the expiration time, once it expires, you must request the server ,

expries appear in the http-equiv attribute, use the content attribute to indicate the expiration time of the page cache

expries=0, the number of minutes before the cache expires. If the user returns to the page before it expires, the cached version of the page will be displayed

1

2

3

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone Learning is helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to use HTML5 File interface is used to download files on the web page

Basic use of postMessage API in HTML5

New HTML5 Eight types of INPUT input

The above is the detailed content of How to use WeChat's embedded H5 web page to solve the problem of JS countdown failure. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

deepseek image generation tutorial deepseek image generation tutorial Feb 19, 2025 pm 04:15 PM

DeepSeek: A powerful AI image generation tool! DeepSeek itself is not an image generation tool, but its powerful core technology provides underlying support for many AI painting tools. Want to know how to use DeepSeek to generate images indirectly? Please continue reading! Generate images with DeepSeek-based AI tools: The following steps will guide you to use these tools: Launch the AI ​​Painting Tool: Search and open a DeepSeek-based AI Painting Tool (for example, search "Simple AI"). Select the drawing mode: select "AI Drawing" or similar function, and select the image type according to your needs, such as "Anime Avatar", "Landscape"

gateio Chinese official website gate.io trading platform website gateio Chinese official website gate.io trading platform website Feb 21, 2025 pm 03:06 PM

Gate.io, a leading cryptocurrency trading platform founded in 2013, provides Chinese users with a complete official Chinese website. The website provides a wide range of services, including spot trading, futures trading and lending, and provides special features such as Chinese interface, rich resources and community support.

List of handling fees for okx trading platform List of handling fees for okx trading platform Feb 15, 2025 pm 03:09 PM

The OKX trading platform offers a variety of rates, including transaction fees, withdrawal fees and financing fees. For spot transactions, transaction fees vary according to transaction volume and VIP level, and adopt the "market maker model", that is, the market charges a lower handling fee for each transaction. In addition, OKX also offers a variety of futures contracts, including currency standard contracts, USDT contracts and delivery contracts, and the fee structure of each contract is also different.

Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance Mar 04, 2025 pm 04:51 PM

Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.

gateio exchange app old version gateio exchange app old version download channel gateio exchange app old version gateio exchange app old version download channel Mar 04, 2025 pm 11:36 PM

Gateio Exchange app download channels for old versions, covering official, third-party application markets, forum communities and other channels. It also provides download precautions to help you easily obtain old versions and solve the problems of discomfort in using new versions or device compatibility.

Ouyi Exchange app domestic download tutorial Ouyi Exchange app domestic download tutorial Mar 21, 2025 pm 05:42 PM

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

How to copy Xiaohongshu copywriting. Graphical tutorial on how to copy Xiaohongshu copywriting. How to copy Xiaohongshu copywriting. Graphical tutorial on how to copy Xiaohongshu copywriting. Jan 16, 2025 pm 04:03 PM

Learn to easily copy Xiaohongshu copywriting! This tutorial teaches you step by step how to quickly copy Xiaohongshu video copy, saying goodbye to tedious steps. Open the Xiaohongshu APP, find the video you like, and click on the [Copywriting] area below the video. Long press the copy text and select the [Extract Text] function from the pop-up options. The system will automatically extract the text, click the [Copy] button in the lower left corner. Open WeChat or other applications, such as Moments, long press the input box, and select [Paste]. Click Send to complete the copy. It's that simple!

What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? What should I do if the company's security software conflicts with applications? How to troubleshoot HUES security software causes common software to fail to open? Apr 01, 2025 pm 10:48 PM

Compatibility issues and troubleshooting methods for company security software and application. Many companies will install security software in order to ensure intranet security. However, security software sometimes...

See all articles