JavaScript-there is no good way to prevent the JS countdown after the page is refreshed

Source: Internet
Author: User
Currently can achieve JS Countdown, but, page a refresh, countdown either directly stop, or directly start again.
so that's the problem?
How to control JS, even if you refresh the page, the countdown is continuous.
Trouble everyone passing by the small partner, do a favor. Thank you!

Reply content:

Currently can achieve JS Countdown, but, page a refresh, countdown either directly stop, or directly start again.
So that's the problem?
How to control JS, even if you refresh the page, the countdown is continuous.
Trouble everyone passing by the small partner, do a favor. Thank you!

Start countdown time to get local time, deposit cookie or localstorage, refresh after read start time and now time want to reduce

Check out the service Worker

Each refresh is a new function running environment and cannot know the state before the refresh. The start time of your timer can only be obtained from the background or calculated based on the current time.

Using cookies or Localstorage,
See a specific answer I wrote.
Http://stackoverflow.com/a/38 ...

3 js:$("#end_time").val()

should be able to read the meaning

When the countdown starts, save the end time to Localstorage, refresh the page to read the Localstorage value and calculate the countdown

Recently the old people asked the countdown question, before writing a simple, the landlord can see

Find a countdown code for every Tuesday

Your countdown start time do not use js to get the client time, use php to get the server time to give a js try

Write a local storage countdown and it's OK.

To achieve a countdown function, first you have to have a fixed end time, the normal logic implementation:

Pass the end time into a countdown method, use the end time minus the current time, get the remaining time, and execute once in a second. The current time will increase, then the remaining time will be reduced accordingly.

So where does the fixed end time come from. 1, the code inside write dead Time 2, back end return time 3, local storage. As long as you get the fixed end time, the countdown will not start from scratch. Interrupts are only seen in the event of a countdown or code error. I wrote a multiple countdown to the simultaneous existence of the page link description

If you want the front-end control can only take advantage of cookie/web storage such as browser storage, so that the same browser does not clear the cache can achieve your needs, if necessary even if the change of the browser will remain unchanged only back-end return time

"Send SMS verification, post button countdown, prevent refresh Countdown expired"
Application scenarios, in the development of "Send SMS Verification" function, to solve a problem, to prevent malicious or frequent SMS verification code issues. The general principle is "when you click the Send button, send an AJAX request to the server to send SMS verification code, the success of the Send button is set to not click, and call, the original link: https://segmentfault.com/a/11 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.