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