Tips for javascript server time synchronization and countdown _ javascript skills

Source: Internet
Author: User
This article provides a solution for synchronously displaying server time on the page, which can synchronize server time and synchronize countdown without occupying too many server resources, the following is my idea of implementation. Someone asked me on the internet how to synchronously display the server time on the page. In fact, there are several implementation methods, most people may immediately think that they can use Ajax to request the server every second and then display the time the server gets on the page. This can be achieved, but there is a big problem, that is to request the server every second, so that if there are more users, the server will crash (the memory usage will be very high), so in my opinion, this method is not feasible, here is a solution that can synchronize server time and synchronize countdown without occupying too much server resources. Implementation ideas:

Step 1,When a user browses the page for the first time, the server first obtains the current time and displays it on the page (for example, in the timebox span with the ID)

Step 2,Set a new time to be calculated every second (the new time takes the server time as the initial value, and then accumulates one second every second and generates a new time)

Step 3,Display the time calculated in step 2

Is it very simple? In a word, the initial value is server time, and then the new time is automatically generated every second on the page, so as to ensure time synchronization with the server, it should be okay if the error is within a few seconds. Let's take a look at the implementation code:

11:21:55 // display the server time for the first time here
Related Article

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.