Before the online to see someone ask questions, how to synchronize the time on the page to display the server, in fact, there are several ways to achieve, it may be common people immediately thought can use Ajax every second to request the server, and then the server gets to the time displayed on the page, although it can be achieved, but there is a big problem, That is every second to request the server, so if the user more, the server will crash (memory occupancy will be very large), so in my opinion, this method is not feasible, I give a solution, can achieve synchronization server time, synchronization countdown, but do not occupy the server too many resources, the following I write implementation of the idea
First, when the user first browses the page, the server first gets the current time and is displayed on the page (for example: displayed in ID timebox span)
The second step, set a new time every second (the new time is the initial value of the server time, and then accumulate a second every second and generate a new time)
Third step, show the time of the second step calculation
is not very simple, summed up into a sentence is: The server time as the initial value, and then on the page automatically every second on the accumulation of a second generation of new time, so that you can ensure synchronization with the server time, the error is basically within a few seconds, it should be okay, well look at the implementation of the Code it:
<span id= "Timebox" >11:21:55</span>//The server time is displayed here for the first time <script type= "Text/javascript" > $ (function () { var otime = $ ("#timebox"); var ts = Otime.text (). Split (":", 3); var tnums = [parseint (Ts[0]), parseint (Ts[1]), parseint (ts[2])]; SetInterval (function () {tnums = Getnexttimenumber (Tnums[0], tnums[1], tnums[2]); Shownewtime (Tnums[0], tnums[1], tnums[2]); }, 1000); function Shownewtime (h, M, s) {var timestr = ("0" + h.tostring ()). substr (-2) + ":" + ("0" + m.tostring ()). substr ( -2) + ":" + ("0" + s.tostring ()). substr (-2); Otime.text (TIMESTR); } function Getnexttimenumber (H, M, s) {if (++s = =) {s = 0; if (s = = 0) {if (++m = =) {m = 0; }} if (M = = 0) {if (++h = = 24) { h = 0; }} return [H, M, S]; }});</script>
The code is very simple here is not much to explain (I only show when the second and second, we can also add the date, plus the date can be in when h==0, directly from the server to obtain a date or the full time, as a time of proofreading), do not understand the following comments, I will promptly reply, Then follow this line of thought to achieve a synchronization countdown, first of all, what is the synchronization countdown, is similar to a second kill, set an end time, and then calculate the current time and end time between the interval, and must be guaranteed on different computers, browsers display the same countdown time , the implementation code is as follows:
<! DOCTYPE html>In order to ensure the accuracy of the countdown, I used the countdown interval to calculate the first unified into seconds, and then 1 seconds to regenerate the time format, of course, you can follow the example of the above time synchronization, direct time reduction, a lot of methods, I am not necessarily the best, welcome to Exchange, thank you!
JavaScript tips: Sync server time, Sync countdown