Time synchronization method, device, electronic device and computer readable storage medium

By receiving server time data on the client and performing time synchronization operations using the timer to determine the second time, the problem of inaccurate time synchronization between the client and the server is solved, ensuring accurate update of time data.

CN115134033BActive Publication Date: 2025-05-16HILLSTONE NETWORKS CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210753844.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-06-29
Publication Date
2025-05-16
Estimated Expiration
2042-06-29

AI Technical Summary

Technical Problem

The time synchronization between the client and the server is inaccurate, resulting in untimely update of time data and subtle deviations accumulate into huge deviations.

Method used

The client sends a time acquisition request to the server, receives time data and determines the first time, performs time synchronization operations based on the timer, determines the second time when successful execution is performed, and calculates the target time data to ensure accuracy.

Benefits of technology

It realizes that the latest server run time can be accurately obtained every time the time synchronization operation is performed on the client and the server according to the timer, improving the accuracy of time synchronization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115134033B_ABST
    Figure CN115134033B_ABST
Patent Text Reader

Abstract

The present application discloses a time synchronization method, device, electronic device and computer-readable storage medium. The method includes: sending a time acquisition request to a server through a client; receiving time data generated by the server according to the time acquisition request through the client, and determining the time when the client receives the time data as the first time, wherein the time data is used to characterize the length of time the server has been running when the time acquisition request is received; performing a time synchronization operation on the client and the server according to a timer, and determining the time when the time synchronization operation is successfully performed as the second time; calculating according to the first time, the second time and the time data to obtain the target time data, wherein the target time data is used to characterize the length of time the server has been running at the second time. The present application solves the technical problem of inaccurate time synchronization between the client and the server in the prior art.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the fields of computer technology and communication technology, and in particular to a time synchronization method, device, electronic device and computer-readable storage medium. Background Art

[0002] In the development of WEB (world wide web, global wide area network), there has always been a demand to display the latest time data of the server through the client web page. Currently, the most common method is that the client obtains the time data of the server by sending an HTTP (hyper text transfer protocol, hypertext transfer protocol) request. After obtaining the time data, the client updates the time data every certain time interval through a timer. For example, the time data is increased by one second every second to obtain the latest time data, and the latest time data is displayed on the client.

[0003] However, since the thread that the client uses to complete the time data update operation through the timer is single-threaded, the time data update operation will be delayed when there are many tasks, which will cause the time data to be updated untimely, and there will be slight deviations between the displayed time data and the actual time data. After multiple delays, these slight deviations will accumulate into huge deviations.

[0004] To address the above-mentioned problems, no effective solution has been proposed yet. Summary of the invention

[0005] The embodiments of the present application provide a time synchronization method, device, electronic device and computer-readable storage medium to at least solve the technical problem of inaccurate time synchronization between a client and a server in the prior art.

[0006] According to one aspect of an embodiment of the present application, a time synchronization method is provided, including: sending a time acquisition request to a server through a client; receiving time data generated by the server according to the time acquisition request through the client, and determining the time when the client receives the time data as a first time, wherein the time data is used to characterize the length of time the server has been running when the time acquisition request is received; performing a time synchronization operation on the client and the server according to a timer, and determining the time when the time synchronization operation is successfully performed as a second time, wherein the timer is a timing program deployed in the client; calculating according to the first time, the second time and the time data to obtain target time data, wherein the target time data is used to characterize the length of time the server has been running at the second time.

[0007] Furthermore, the time synchronization method also includes: obtaining the minimum display time of the client's display page for a page display; determining a preset interval time based on the minimum display time; performing a time synchronization operation on the client and the server once every preset interval time according to the timer, and determining the time when the time synchronization operation is successfully executed as the second time.

[0008] Furthermore, the time synchronization method also includes: when a time synchronization operation is performed on the client and the server once at a preset interval according to the timer, detecting whether the time synchronization operation is successfully performed; when the time synchronization operation is not successfully performed, continuously performing the time synchronization operation on the client and the server according to the timer until the time synchronization operation is successfully performed, and determining the time when the time synchronization operation is successfully performed as the second time.

[0009] Furthermore, the time synchronization method further includes: in case of detecting that the time synchronization operation is not performed, determining that the time synchronization operation is not successfully performed.

[0010] Furthermore, the time synchronization method further includes: in case of detecting that the time synchronization operation fails to be executed, determining that the time synchronization operation is not successfully executed.

[0011] Furthermore, the time synchronization method also includes: calculating the difference between the second time and the first time to obtain a target interval duration; and summing the target interval duration and the time data to obtain target time data.

[0012] Furthermore, the time synchronization method further includes: after obtaining target time data by calculation according to the first time, the second time and the time data, the target time data is sent to a display page for display.

[0013] According to another aspect of an embodiment of the present application, a time synchronization device is also provided, including: a sending module, used to send a time acquisition request to a server through a client; a receiving module, used to receive time data generated by the server according to the time acquisition request through the client, and determine the time when the client receives the time data as a first time, wherein the time data is used to characterize the length of time the server has been running when the time acquisition request is received; a determination module, used to perform a time synchronization operation on the client and the server according to a timer, and determine the time when the time synchronization operation is successfully performed as a second time, wherein the timer is a timing program deployed in the client; a calculation module, used to perform calculations based on the first time, the second time and the time data to obtain target time data, wherein the target time data is used to characterize the length of time the server has been running at the second time.

[0014] According to another aspect of an embodiment of the present application, a computer-readable storage medium is further provided, in which a computer program is stored, wherein the computer program is configured to execute the above-mentioned time synchronization method when running.

[0015] According to another aspect of an embodiment of the present application, an electronic device is also provided, which includes one or more processors; a storage device for storing one or more programs, which, when the one or more programs are executed by the one or more processors, enables the one or more processors to run the programs, wherein the programs are configured to execute the above-mentioned time synchronization method when running.

[0016] In an embodiment of the present application, the time when the time synchronization operation is successfully executed is determined as the second time, and the target time data is calculated based on the first time, the second time and the time data. First, a time acquisition request is sent to the server through the client; then the time data generated by the server according to the time acquisition request is received by the client, and the time when the client receives the time data is determined as the first time, then the time synchronization operation is performed on the client and the server according to the timer, and the time when the time synchronization operation is successfully executed is determined as the second time, and finally, the target time data is calculated based on the first time, the second time and the time data, wherein the time data is used to represent the length of time the server has been running when the time acquisition request is received, the timer is a timing program deployed in the client, and the target time data is used to represent the length of time the server has been running at the second time.

[0017] From the above content, it can be seen that the present application determines the time when the timer successfully executes the time synchronization operation as the second time, ensuring that the target time data is calculated based on the second time only when the time synchronization operation has been successfully executed. Therefore, the problem of inaccurate time synchronization caused by delayed execution of the time synchronization operation can be avoided, and the effect of accurately obtaining the latest server running time each time the time synchronization operation is executed on the client and the server based on the timer is achieved.

[0018] It can be seen that through the technical solution of the present application, the purpose of accurately displaying the latest time data of the server on the client is achieved, thereby achieving the technical effect of improving the accuracy of time synchronization between the client and the server, and further solving the technical problem of inaccurate time synchronization between the client and the server in the prior art. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:

[0020] Figure 1 is a flowchart of an optional time synchronization method according to an embodiment of the present application;

[0021] Figure 2 is a flowchart of another optional time synchronization method according to an embodiment of the present application;

[0022] Figure 3 is a flow chart of updating time data according to an embodiment of the present application;

[0023] Figure 4 is a schematic diagram of an optional time synchronization method according to the prior art;

[0024] Figure 5 is a schematic diagram of an optional time synchronization method according to an embodiment of the present application;

[0025] Figure 6 It is a comparative schematic diagram based on two time synchronization methods;

[0026] Figure 7 It is a schematic diagram of an optional time synchronization device according to an embodiment of the present application. DETAILED DESCRIPTION

[0027] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.

[0028] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0029] Example 1

[0030] In WEB development, there is always a need to display the latest time data of the server on the web page. Currently, the most common method is for the client to obtain the time data of the server by sending an HTTP request. After obtaining the time data, the client updates the time data every certain time interval through a timer. For example, the time data is increased by one second every second to obtain the latest time data, and the latest time data is displayed on the client.

[0031] However, in the prior art, the client usually uses JavaScript (a browser scripting language) to call the timer, thereby realizing the time synchronization operation for updating the time data between the client and the server. Therefore, if the time data is to be updated in a timely manner, it is necessary to ensure that JavaScript can successfully perform the time synchronization operation in the first place. However, since JavaScript is single-threaded, JavaScript cannot execute multiple sections of code at the same time. When JavaScript is executing a certain section of code, all subsequent tasks must wait in line, which will cause task blocking.

[0032] For example, if JavaScript is currently executing a time-consuming code, and the timer originally set is to update the time data every 1 second, and the time interval between the last time data update and the last time data update has reached 1 second, according to the original design, the time data needs to be updated immediately, that is, the original time data is added with the preset time interval (here 1 second) to obtain the new time data and display the new time data. However, since JavaScript is executing other tasks at this time, the task of updating the time data can only wait in line. Assuming that it is 0.05 seconds before the task of updating the time data is executed, then the new time data obtained by the client at this time is 0.05 seconds slower than the actual time data of the server. In other words, there is a 0.05 second deviation between the time data displayed by the client at this time and the actual time data of the server. With the accumulation of multiple small deviations, the time data may deviate for several minutes or even hours in the future.

[0033] In order to solve the above-mentioned problems in the prior art, according to an embodiment of the present application, a method embodiment of a time synchronization method is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.

[0034] Figure 1 is a flowchart of an optional time synchronization method according to an embodiment of the present application, such as Figure 1 As shown, the method comprises the following steps:

[0035] Step S101: Send a time acquisition request to a server through the client.

[0036] In step S101, the time acquisition request may be an HTTP request including a time acquisition instruction, the client includes but is not limited to terminal devices such as smart phones, smart tablets, notebook computers, desktop computers, and smart wearable devices, and the server may be a local server or a cloud server. The time acquisition request includes a time acquisition instruction, and after receiving the time acquisition request, the server responds to the time acquisition instruction and returns the current time data of the server to the client.

[0037] Step S102: receiving, through the client, time data generated by the server according to the time acquisition request, and determining the time when the client receives the time data as the first time.

[0038] In step S102, the time data is used to represent the length of time the server has been running when receiving the time acquisition request. For the convenience of description, the time data is represented by T and the first time is represented by t0.

[0039] Step S103: performing a time synchronization operation on the client and the server according to the timer, and determining the time when the time synchronization operation is successfully performed as a second time.

[0040] In step S103, the timer is a timing program deployed in the client. The client calls the timer through JavaScript to perform time synchronization operations on the client and the server. The time synchronization operation can also be understood as an update operation on the time data T. It should be noted that the above-mentioned second time in this application is the time when the time synchronization operation is successfully executed. In the case where JavaScript has not yet called the timer to perform the time synchronization operation on the client and the server, the time synchronization operation cannot be determined as successfully executed. In other words, when JavaScript is blocked by a task and the timer cannot be called immediately to perform the time synchronization operation, the client in this application will determine the time synchronization operation at this time as an unsuccessful execution state. Only when JavaScript calls the timer to successfully perform the time synchronization operation, this application will determine the time synchronization operation as a successfully executed state, and the time when the time synchronization is successfully executed is determined as the second time. In addition, for the convenience of description, the second time is represented by t1 below.

[0041] Step S104, performing calculation according to the first time, the second time and the time data to obtain target time data.

[0042] In step S104, the target time data is used to characterize the duration of the server running at the second time. After the target time data is obtained by calculation based on the first time, the second time and the time data, the client will also send the target time data to the display page for display.

[0043] For the convenience of description, the target time data is represented by T1 below. Specifically, after obtaining the first time t0, the second time t1 and the time data T, the client first calculates the difference between the second time and the first time to obtain the target interval duration. The calculation formula is as follows:

[0044] Δt=t1-t0

[0045] Among them, Δt is the target interval duration.

[0046] Then, the client sums the target interval duration and the time data to obtain the target time data. The calculation formula is as follows:

[0047] T1=T+Δt

[0048] In order to more clearly illustrate the solution in the embodiment of the present application, the time synchronization method in the embodiment of the present application is described below by using an example.

[0049] Optionally, the client first sends a time acquisition request to the server, and records the first time t0 when the client receives the time data T returned by the server, where the time data T represents the length of time the server has been running when receiving the time acquisition request, for example, the time data T is 2 hours, 25 minutes and 26 seconds.

[0050] In order to accurately and dynamically display the real-time running time of the server on the display page of the client, the client needs to update the time data T, that is, perform time synchronization operations on the client and the server. Specifically, the client can perform the update processing of the time data through the timer pre-deployed in the client. The timer requires two parameters. One parameter is the preset interval length of updating the time data. Assuming that the minimum unit of time for the display page of the client to display a page is seconds, the preset interval length can be set to 1 second, that is, the client performs a time synchronization operation on the client and the server every 1 second according to the timer, that is, updates the time data T once; the other parameter is the time data update function used by the client when performing the time synchronization operation on the client and the server according to the timer. The specific processing logic of the time data update function is to obtain the second time t1 when the time synchronization operation is successfully executed, and then calculate the target interval length Δt between the second time t1 and the first time t0 by the formula Δt=t1-t0, and finally add the initially obtained time data T (2 hours, 25 minutes and 26 seconds) to Δt according to the formula T1=T+Δt to obtain the latest time data (i.e., target time data). For example, if 4 seconds have passed since the server returned the time data when the time synchronization operation is successfully executed, the target time data obtained by executing the time data update function should be 2 hours, 25 minutes and 30 seconds (2 hours, 25 minutes and 26 seconds + 4 seconds).

[0051] In addition, after the timer is set in the client, the present application will update the time data once every preset interval (that is, perform time synchronization operations on the client and the server), and the second time t1 used by the client when executing the time data update function is the time when the timer successfully executes the time synchronization operation, thereby ensuring that the client will only start calculating the target time data according to the second time when the time synchronization operation has been successfully executed, thereby avoiding the problem of inaccurate time synchronization caused by delayed execution of the time synchronization operation, and ensuring that the target time data obtained by the client each time is accurate.

[0052] It can be seen from the contents of the above steps S101 to S104 that in an embodiment of the present application, the time when the time synchronization operation is successfully executed is determined as the second time, and the target time data is calculated based on the first time, the second time and the time data. First, a time acquisition request is sent to the server through the client; then the time data generated by the server according to the time acquisition request is received by the client, and the time when the client receives the time data is determined as the first time, then the time synchronization operation is performed on the client and the server according to the timer, and the time when the time synchronization operation is successfully executed is determined as the second time, and finally, the target time data is calculated based on the first time, the second time and the time data, wherein the time data is used to characterize the length of time the server has been running when the time acquisition request is received, the timer is a timing program deployed in the client, and the target time data is used to characterize the length of time the server has been running at the second time.

[0053] From the above content, it can be seen that the present application determines the time when the timer successfully executes the time synchronization operation as the second time, ensuring that the target time data is calculated based on the second time only when the time synchronization operation has been successfully executed. Therefore, the problem of inaccurate time synchronization caused by delayed execution of the time synchronization operation can be avoided, and the effect of accurately obtaining the latest server running time each time the time synchronization operation is executed on the client and the server based on the timer is achieved.

[0054] It can be seen that through the technical solution of the present application, the purpose of accurately displaying the latest time data of the server on the client is achieved, thereby achieving the technical effect of improving the accuracy of time synchronization between the client and the server, and further solving the technical problem of inaccurate time synchronization between the client and the server in the prior art.

[0055] In an optional embodiment, the client obtains the minimum display time for displaying a page, and then determines a preset interval time based on the minimum display time. Finally, the client performs a time synchronization operation on the client and the server every preset interval time according to the timer, and determines the time when the time synchronization operation is successfully executed as the second time.

[0056] Optionally, the minimum display duration for a page to be displayed once can be understood as the minimum time unit for a page to be displayed once, for example, seconds or minutes. Generally speaking, if the minimum display duration is 1 second, the preset interval duration can be set to 1 second, and if the minimum display duration is 1 minute, the preset interval duration can be set to 1 minute. In some application scenarios, the preset interval duration can also be greater than or less than the minimum display duration, and this application does not make any special restrictions on the setting of the preset interval duration.

[0057] In an optional embodiment, when a time synchronization operation is performed on the client and the server once at a preset interval according to the timer, the client will detect whether the time synchronization operation is successfully executed; when the time synchronization operation is not successfully executed, the client will continue to execute the time synchronization operation on the client and the server according to the timer until the time synchronization operation is successfully executed, and determine the time when the time synchronization operation is successfully executed as the second time.

[0058] Optionally, in the case where it is detected that the time synchronization operation is not performed, the client will determine that the time synchronization operation is not successfully performed, and in the case where it is detected that the time synchronization operation fails to perform, the client will also determine that the time synchronization operation is not successfully performed.

[0059] Specifically, each time a time synchronization operation is performed on the client and the server according to the timer, the client calls the timer through JavaScript, but because JavaScript is single-threaded, JavaScript cannot execute multiple code segments at the same time. When JavaScript is executing a certain code segment, all subsequent tasks must wait in line, which will cause task blocking. On this basis, if JavaScript has not yet called the timer to perform a time synchronization operation on the client and the server, that is, the time synchronization operation has not yet been performed, the client in this application will determine the time synchronization operation at this time as an unsuccessful execution state. In addition, if JavaScript calls the timer to perform a time synchronization operation on the client and the server, but an exception occurs during the call process, causing the time synchronization operation to fail, the client in this application will also determine the time synchronization operation at this time as an unsuccessful execution state. The client in this application will only determine the time synchronization operation as a successfully executed state when JavaScript calls the timer to successfully perform the time synchronization operation, and determine the time when the time synchronization is successfully executed as the second time.

[0060] In an optional embodiment, Figure 2 A flowchart of another optional time synchronization method according to an embodiment of the present application is shown. Figure 2As shown, the present application mainly re-conceives the updating process of time data based on the timer, starting from the identity relationship that the difference between the server time and the client time is always unchanged, the entire time data maintenance plan is mainly designed around the identities Δt=t1-t0, T1=T+Δt. First, the client sends a time acquisition request to the server, and then the server returns the current time data to the client after receiving the time acquisition request. The client records the time when the time data is received as t0, and records the time data as T. The client performs a time synchronization operation on the client and the server once according to the preset interval length of the timer, that is, updates the time data T once. After each update process, the client will obtain a target time data and display the target time data on the display page.

[0061] Optional, such as Figure 3 As shown, when the client updates the time data T, it will record the time when the time synchronization operation is successfully executed as t1. Combined with the time data T previously recorded by the client and the first time t0 when the client receives the time data, the client will first calculate the difference between t1 and t0 to obtain the target interval duration Δt, where Δt represents the time interval between the client obtaining the time data and executing the time synchronization operation. Finally, the client adds the time data T and the target interval duration Δt to obtain the target time data.

[0062] The following is a comparison with the prior art in conjunction with the accompanying drawings to illustrate the difference between the time synchronization method in the embodiment of the present application and the prior art.

[0063] Assuming that the client's display time is accurate to 0.1 seconds, the client needs to update the time data and display it every 1 second through a timer.

[0064] Among them, the time data update processing logic in the prior art is to add the previous time data to the preset time interval to obtain the new time data and display it. Without considering the task blocking that may occur when JavaScript calls the timer to update the time data, the execution process is as follows: Figure 4 As shown:

[0065] The client receives the time data T returned by the server at t0, and triggers the timer to perform a time synchronization operation at t0+1 second, that is, to update the time data T. Since the task queue of JavaScript in the client is empty at this time, the JavaScript thread can immediately call the timer to update the time data T, and add the time data T to the preset interval of 1 second to obtain the updated time data T1, where T1=T+1 second; similarly, at t0+2 seconds, the client triggers the timer to perform a time synchronization operation, that is, to update the previous time data T1. Since the task queue of JavaScript is still empty at this time, JavaScript can also immediately call the timer to update the time data T1, and add the time data T1 to the preset interval of 1 second to obtain the updated time data T2, where T2=T1+1 second.

[0066] and Figure 4 Compare the solutions in Figure 5 FIG. 1 is a schematic diagram showing a time synchronization method according to an embodiment of the present application. Figure 5 As shown, the client receives the time data T returned by the server at t0, and triggers the timer to perform the time synchronization operation at t0+1 seconds, that is, to update the time data T. Since the task queue of JavaScript in the client is empty at this time, the JavaScript thread can immediately call the timer to update the time data T. The client will record the time when the time synchronization operation is successfully executed as t1, and then calculate the target interval duration Δt according to the formula Δt=t1-t0, and finally obtain the target time data T1 according to the formula T1=T+Δt, where t1 can be understood as the time when the time data T is actually successfully updated. Similarly, at t0+2 seconds, the client triggers the timer again to perform the time synchronization operation. Since the task queue of JavaScript in the client is still empty at this time, the JavaScript thread can also immediately call the timer to update the time data T. The client will record the time when the time synchronization operation is successfully executed as the new t1, and then calculate the target interval duration Δt according to the formula Δt=t1-t0, and finally obtain the new target time data T1 according to the formula T1=T+Δt.

[0067] From the above content, it can be seen that, without considering the task blocking that occurs when JavaScript calls the timer to update the time data, both solutions can accurately achieve the time synchronization between the client and the server. However, in the client's display page, the display of time information is often only part of the display function, and the execution of other functions is likely to block the update of the time data. The following is combined with the accompanying drawings to show the execution process of the prior art and the technical solution of this application after encountering two blockages within a period of time.

[0068] like Figure 6 As shown, in Figure 6 In the example, the client receives the time data T returned by the server at t0. At t0+1 second, according to the pre-designed process, the timer needs to be triggered to perform time synchronization operations on the client and the server, that is, to update the time data T. However, since JavaScript is processing other tasks at this time, JavaScript cannot immediately call the timer to update the time data T, and needs to wait in line. Assuming that the waiting time is 0.5 seconds, the time synchronization operation is successfully executed at t0+1.5 seconds. According to the solution of the prior art, the updated time data is T1=T+1 second, where, assuming that T is 10 seconds, T1 is 11 seconds. According to the time synchronization method of the present application, first determine that the time when the time synchronization operation is successfully executed is t1, that is, t1=t0+1.5 seconds, and then calculate the target interval duration Δt=1.5 seconds according to the formula Δt=t1-t0, and finally obtain the target time data T1=11.5 seconds according to the formula T1=T+Δt.

[0069] Furthermore, if Figure 6 As shown, at t0+2.5 seconds, the client needs to trigger the timer again to perform the time synchronization operation. However, since the JavaScript in the client still cannot call the timer immediately at this time, the related operations of updating the time data still need to wait in line. Assuming that the waiting time is still 0.5 seconds, the time synchronization operation is successfully executed at t0+3 seconds. According to the solution of the prior art, the updated time data is T2=T1+1 second, that is, T2 is 12 seconds. According to the time synchronization method of the present application, first determine the time when the time synchronization operation is successfully executed as the new t1, that is, the new t1=t0+3 seconds, and then calculate the target interval duration Δt=3 seconds according to the formula Δt=t1-t0, and finally obtain the target time data T1=10 seconds+3 seconds=13 seconds according to the formula T1=T+Δt.

[0070] From the above content, it can be seen that the prior art has a 0.5 second deviation between the time data displayed after encountering a blockage (i.e., the time synchronization operation is delayed) and the time data that should actually be displayed. After encountering a second blockage, another 0.5 second deviation occurs. The accumulation of the two deviations causes the final displayed time data to be 1 second slower than the time data that should actually be displayed. This situation shows that according to the time synchronization method of the prior art, if a blockage is encountered, small deviations will occur, and these small deviations will accumulate into huge deviations. According to the technical solution of the present application, when encountering a blockage, the processing logic of the time data update function can be used to prevent the occurrence of small deviations, thereby avoiding the problem of small deviation accumulation, and thus achieving the effect of improving the accuracy of time synchronization between the server and the client.

[0071] In addition, since the technical solution in this application only needs to maintain the time data update function to achieve the purpose of accurately maintaining time data, this application does not need to store too many time differences in the client, thereby simplifying the code complexity and improving the processing efficiency of time synchronization operations between the client and the server.

[0072] Example 2

[0073] According to an embodiment of the present application, a time synchronization device embodiment is also provided, wherein: Figure 7 is a schematic diagram of an optional time synchronization device according to an embodiment of the present application, such as Figure 7 As shown, the device includes: a sending module 701, a receiving module 702, a determining module 703 and a calculating module 704.

[0074] The sending module 701 is used to send a time acquisition request to the server through the client.

[0075] Specifically, the time acquisition request may be an HTTP request including a time acquisition instruction, the client includes but is not limited to terminal devices such as smart phones, smart tablets, notebook computers, desktop computers, and smart wearable devices, and the server may be a local server or a cloud server. The time acquisition request includes a time acquisition instruction, and after receiving the time acquisition request, the server responds to the time acquisition instruction and returns the current time data of the server to the client.

[0076] A receiving module 702 is configured to receive, through a client, time data generated by a server according to a time acquisition request, and determine the time when the client receives the time data as a first time, wherein the time data is used to represent the length of time the server has been running when receiving the time acquisition request;

[0077] Specifically, for the convenience of description, the time data is represented by T and the first time is represented by t0.

[0078] The determination module 703 is used to perform a time synchronization operation on the client and the server according to the timer, and determine the time when the time synchronization operation is successfully performed as the second time, wherein the timer is a timing program deployed in the client.

[0079] Specifically, the client calls the timer through JavaScript to perform a time synchronization operation on the client and the server, and the time synchronization operation can also be understood as an update operation on the time data T. It should be noted that the above-mentioned second time in this application is the time when the time synchronization operation is successfully executed. Among them, in the case where JavaScript has not called the timer to perform the time synchronization operation on the client and the server, the time synchronization operation cannot be determined as successfully executed. In other words, in the case where JavaScript is blocked by a task and the timer cannot be called immediately to perform the time synchronization operation, the client in this application will determine the time synchronization operation at this time as a state of unsuccessful execution. Only when JavaScript calls the timer to successfully perform the time synchronization operation, this application will determine the time synchronization operation as a state of successful execution, and determine the time when the time synchronization is successfully executed as the second time. In addition, for the convenience of description, the second time is represented by t1 below. The calculation module 704 is used to calculate according to the first time, the second time and the time data to obtain the target time data, wherein the target time data is used to characterize the length of time the server has been running at the second time.

[0080] In an optional embodiment, after obtaining the target time data through calculation according to the first time, the second time and the time data, the client will also send the target time data to a display page for display.

[0081] For the convenience of description, the target time data is represented by T1 below. Specifically, after obtaining the first time t0, the second time t1 and the time data T, the client first calculates the difference between the second time and the first time to obtain the target interval duration. The calculation formula is as follows:

[0082] Δt=t1-t0

[0083] Among them, Δt is the target interval duration.

[0084] Then, the client sums the target interval duration and the time data to obtain the target time data. The calculation formula is as follows:

[0085] T1=T+Δt

[0086] In order to more clearly illustrate the solution in the embodiment of the present application, the time synchronization method in the embodiment of the present application is described below by using an example.

[0087] Optionally, the client first sends a time acquisition request to the server, and records the first time t0 when the client receives the time data T returned by the server, where the time data T represents the length of time the server has been running when receiving the time acquisition request, for example, the time data T is 2 hours, 25 minutes and 26 seconds.

[0088] In order to accurately and dynamically display the real-time running time of the server on the display page of the client, the client needs to update the time data T, that is, perform time synchronization operations on the client and the server. Specifically, the client can perform the update processing of the time data through the timer pre-deployed in the client. The timer requires two parameters. One parameter is the preset interval length of updating the time data. Assuming that the minimum unit of time for the display page of the client to display a page is seconds, the preset interval length can be set to 1 second, that is, the client performs a time synchronization operation on the client and the server every 1 second according to the timer, that is, updates the time data T once; the other parameter is the time data update function used by the client when performing the time synchronization operation on the client and the server according to the timer. The specific processing logic of the time data update function is to obtain the second time t1 when the time synchronization operation is successfully executed, and then calculate the target interval length Δt between the second time t1 and the first time t0 by the formula Δt=t1-t0, and finally add the initially obtained time data T (2 hours, 25 minutes and 26 seconds) to Δt according to the formula T1=T+Δt to obtain the latest time data (i.e., target time data). For example, if 4 seconds have passed since the server returned the time data when the time synchronization operation is successfully executed, the target time data obtained by executing the time data update function should be 2 hours, 25 minutes and 30 seconds (2 hours, 25 minutes and 26 seconds + 4 seconds).

[0089] In addition, after the timer is set in the client, the present application will update the time data once every preset interval (that is, perform time synchronization operations on the client and the server), and the second time t1 used by the client when executing the time data update function is the time when the timer successfully executes the time synchronization operation, thereby ensuring that the client will only start calculating the target time data according to the second time when the time synchronization operation has been successfully executed, thereby avoiding the problem of inaccurate time synchronization caused by delayed execution of the time synchronization operation, and ensuring that the target time data obtained by the client each time is accurate.

[0090] From the above content, it can be seen that the present application determines the time when the timer successfully executes the time synchronization operation as the second time, ensuring that the target time data is calculated based on the second time only when the time synchronization operation has been successfully executed. Therefore, the problem of inaccurate time synchronization caused by delayed execution of the time synchronization operation can be avoided, and the effect of accurately obtaining the latest server running time each time the time synchronization operation is executed on the client and the server based on the timer is achieved.

[0091] It can be seen that, through the technical solution of the present application, the purpose of accurately displaying the latest time data of the server on the client is achieved, thereby achieving the technical effect of improving the accuracy of time synchronization between the client and the server, and further solving the technical problem of inaccurate time synchronization between the client and the server in the prior art. Optionally, the above-mentioned determination module also includes: a first acquisition unit, a first determination unit, and a second determination unit. Among them, the first acquisition unit is used to obtain the minimum display time of the display page of the client for a page display; the first determination unit is used to determine the preset interval time according to the minimum display time; the second determination unit is used to perform a time synchronization operation on the client and the server according to the preset interval time of the timer every time, and determine the time when the time synchronization operation is successfully executed as the second time.

[0092] Optionally, the minimum display duration for a page to be displayed once can be understood as the minimum time unit for a page to be displayed once, for example, seconds or minutes. Generally speaking, if the minimum display duration is 1 second, the preset interval duration can be set to 1 second, and if the minimum display duration is 1 minute, the preset interval duration can be set to 1 minute. In some application scenarios, the preset interval duration can also be greater than or less than the minimum display duration, and this application does not make any special restrictions on the setting of the preset interval duration.

[0093] Optionally, the time synchronization device further includes: a detection module and a first determination module. The detection module is used to detect whether the time synchronization operation is successfully performed when the time synchronization operation is performed on the client and the server once at a preset interval according to the timer; the first determination module is used to continuously perform the time synchronization operation on the client and the server according to the timer when the time synchronization operation is not successfully performed until the time synchronization operation is successfully performed, and determine the time when the time synchronization operation is successfully performed as the second time.

[0094] Optionally, the above detection module further includes: a third determination unit, configured to determine that the time synchronization operation is not successfully executed when it is detected that the time synchronization operation is not executed.

[0095] Optionally, the above detection module further includes: a fourth determination unit, configured to determine that the time synchronization operation is not successfully executed when it is detected that the time synchronization operation fails to execute.

[0096] Specifically, each time a time synchronization operation is performed on the client and the server according to the timer, the client calls the timer through JavaScript, but because JavaScript is single-threaded, JavaScript cannot execute multiple code segments at the same time. When JavaScript is executing a certain code segment, all subsequent tasks must wait in line, which will cause task blocking. On this basis, if JavaScript has not yet called the timer to perform a time synchronization operation on the client and the server, that is, the time synchronization operation has not yet been performed, the client in this application will determine the time synchronization operation at this time as an unsuccessful execution state. In addition, if JavaScript calls the timer to perform a time synchronization operation on the client and the server, but an exception occurs during the call process, causing the time synchronization operation to fail, the client in this application will also determine the time synchronization operation at this time as an unsuccessful execution state. The client in this application will only determine the time synchronization operation as a successfully executed state when JavaScript calls the timer to successfully perform the time synchronization operation, and determine the time when the time synchronization is successfully executed as the second time.

[0097] Optionally, the above-mentioned calculation module also includes: a first calculation unit and a second calculation unit, wherein the first calculation unit is used to calculate the difference between the second time and the first time to obtain the target interval duration; the second calculation unit is used to sum the target interval duration and the time data to obtain the target time data.

[0098] Optionally, the time synchronization device further includes: a display module, configured to send the target time data to a display page for display.

[0099] In an optional embodiment, Figure 2 A flowchart of another optional time synchronization method according to an embodiment of the present application is shown. Figure 2As shown, the present application mainly re-conceives the updating process of time data based on the timer, starting from the identity relationship that the difference between the server time and the client time is always unchanged, the entire time data maintenance plan is mainly designed around the identities Δt=t1-t0, T1=T+Δt. First, the client sends a time acquisition request to the server, and then the server returns the current time data to the client after receiving the time acquisition request. The client records the time when the time data is received as t0, and records the time data as T. The client performs a time synchronization operation on the client and the server once according to the preset interval length of the timer, that is, updates the time data T once. After each update process, the client will obtain a target time data and display the target time data on the display page.

[0100] Optional, such as Figure 3 As shown, when the client updates the time data T, it will record the time when the time synchronization operation is successfully executed as t1. Combined with the time data T previously recorded by the client and the first time t0 when the client receives the time data, the client will first calculate the difference between t1 and t0 to obtain the target interval duration Δt, where Δt represents the time interval between the client obtaining the time data and executing the time synchronization operation. Finally, the client adds the time data T and the target interval duration Δt to obtain the target time data.

[0101] The following is a comparison with the prior art in conjunction with the accompanying drawings to illustrate the difference between the time synchronization method in the embodiment of the present application and the prior art.

[0102] Assuming that the client's display time is accurate to 0.1 seconds, the client needs to update the time data and display it every 1 second through a timer.

[0103] Among them, the time data update processing logic in the prior art is to add the previous time data to the preset time interval to obtain the new time data and display it. Without considering the task blocking that may occur when JavaScript calls the timer to update the time data, the execution process is as follows: Figure 4 As shown:

[0104] The client receives the time data T returned by the server at t0, and triggers the timer to perform a time synchronization operation at t0+1 second, that is, to update the time data T. Since the task queue of JavaScript in the client is empty at this time, the JavaScript thread can immediately call the timer to update the time data T, and add the time data T to the preset interval of 1 second to obtain the updated time data T1, where T1=T+1 second; similarly, at t0+2 seconds, the client triggers the timer to perform a time synchronization operation, that is, to update the previous time data T1. Since the task queue of JavaScript is still empty at this time, JavaScript can also immediately call the timer to update the time data T1, and add the time data T1 to the preset interval of 1 second to obtain the updated time data T2, where T2=T1+1 second.

[0105] and Figure 4 Compare the solutions in Figure 5 FIG. 1 is a schematic diagram showing a time synchronization method according to an embodiment of the present application. Figure 5 As shown, the client receives the time data T returned by the server at t0, and triggers the timer to perform the time synchronization operation at t0+1 seconds, that is, to update the time data T. Since the task queue of JavaScript in the client is empty at this time, the JavaScript thread can immediately call the timer to update the time data T. The client will record the time when the time synchronization operation is successfully executed as t1, and then calculate the target interval duration Δt according to the formula Δt=t1-t0, and finally obtain the target time data T1 according to the formula T1=T+Δt, where t1 can be understood as the time when the time data T is actually successfully updated. Similarly, at t0+2 seconds, the client triggers the timer again to perform the time synchronization operation. Since the task queue of JavaScript in the client is still empty at this time, the JavaScript thread can also immediately call the timer to update the time data T. The client will record the time when the time synchronization operation is successfully executed as the new t1, and then calculate the target interval duration Δt according to the formula Δt=t1-t0, and finally obtain the new target time data T1 according to the formula T1=T+Δt.

[0106] From the above content, it can be seen that, without considering the task blocking that occurs when JavaScript calls the timer to update the time data, both solutions can accurately achieve the time synchronization between the client and the server. However, in the client's display page, the display of time information is often only part of the display function, and the execution of other functions is likely to block the update of the time data. The following is combined with the accompanying drawings to show the execution process of the prior art and the technical solution of this application after encountering two blockages within a period of time.

[0107] like Figure 6 As shown, in Figure 6 In the embodiment, scheme 1 is a time synchronization scheme in the prior art, and scheme 2 is a time synchronization scheme provided by the embodiment of the present application. The client receives the time data T returned by the server at t0. At t0+1 seconds, according to the pre-designed process, it is necessary to trigger the timer to perform the time synchronization operation on the client and the server, that is, to update the time data T. However, since JavaScript is processing other tasks at this time, JavaScript cannot immediately call the timer to update the time data T, and needs to wait in line. Assuming that the waiting time is 0.5 seconds, the time synchronization operation is successfully executed at t0+1.5 seconds. According to the scheme of the prior art, the updated time data is T1=T+1 seconds, where, assuming that T is 10 seconds, T1 is 11 seconds. According to the time synchronization method of the present application, first determine that the time when the time synchronization operation is successfully executed is t1, that is, t1=t0+1.5 seconds, and then calculate the target interval duration Δt=1.5 seconds according to the formula Δt=t1-t0, and finally obtain the target time data T1=11.5 seconds according to the formula T1=T+Δt.

[0108] Furthermore, if Figure 6 As shown, at t0+2.5 seconds, the client needs to trigger the timer again to perform the time synchronization operation. However, since the JavaScript in the client still cannot call the timer immediately at this time, the related operations of updating the time data still need to wait in line. Assuming that the waiting time is still 0.5 seconds, the time synchronization operation is successfully executed at t0+3 seconds. According to the solution of the prior art, the updated time data is T2=T1+1 second, that is, T2 is 12 seconds. According to the time synchronization method of the present application, first determine the time when the time synchronization operation is successfully executed as the new t1, that is, the new t1=t0+3 seconds, and then calculate the target interval duration Δt=3 seconds according to the formula Δt=t1-t0, and finally obtain the target time data T1=10 seconds+3 seconds=13 seconds according to the formula T1=T+Δt.

[0109] From the above content, it can be seen that the prior art has a 0.5 second deviation between the time data displayed after encountering a blockage (i.e., the time synchronization operation is delayed) and the time data that should actually be displayed. After encountering a second blockage, another 0.5 second deviation occurs. The accumulation of the two deviations causes the final displayed time data to be 1 second slower than the time data that should actually be displayed. This situation shows that according to the time synchronization method of the prior art, if a blockage is encountered, small deviations will occur, and these small deviations will accumulate into huge deviations. According to the technical solution of the present application, when encountering a blockage, the processing logic of the time data update function can be used to prevent the occurrence of small deviations, thereby avoiding the problem of small deviation accumulation, and thus achieving the effect of improving the accuracy of time synchronization between the server and the client.

[0110] In addition, since the technical solution in this application only needs to maintain the time data update function to achieve the purpose of accurately maintaining time data, this application does not need to store too many time differences in the client, thereby simplifying the code complexity and improving the processing efficiency of time synchronization operations between the client and the server.

[0111] Example 3

[0112] According to another aspect of an embodiment of the present application, a computer-readable storage medium is further provided, in which a computer program is stored, wherein the computer program is configured to execute the time synchronization method in the above-mentioned embodiment 1 when running.

[0113] Example 4

[0114] According to another aspect of an embodiment of the present application, an electronic device is also provided, which includes one or more processors; a storage device for storing one or more programs, when the one or more programs are executed by one or more processors, the one or more processors are implemented to run the programs, wherein the programs are configured to execute the time synchronization method in the above-mentioned embodiment 1 when running.

[0115] The serial numbers of the above-mentioned embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.

[0116] In the above embodiments of the present application, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, please refer to the relevant description of other embodiments.

[0117] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of units can be a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.

[0118] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed over multiple units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.

[0119] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0120] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art or all or part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, server or network device, etc.) to perform all or part of the steps of each embodiment method of the present application. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, disk or optical disk, etc. Various media that can store program codes.

[0121] The above are only preferred implementations of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.

Claims

1. A time synchronization method, characterized in that: include: Send a time acquisition request to the server through the client; Receiving, by the client, time data generated by the server according to the time acquisition request, and determining the time when the client receives the time data as a first time, wherein the time data is used to represent the length of time that the server has been running when receiving the time acquisition request; Performing a time synchronization operation on the client and the server according to a timer, and determining the time when the time synchronization operation is successfully performed as a second time, wherein the timer is a timing program deployed in the client; Calculating according to the first time, the second time and the time data to obtain target time data, wherein the target time data is used to represent the length of time the server has been running at the second time; Among them, calculating according to the first time, the second time and the time data to obtain the target time data includes: calculating the difference between the second time and the first time to obtain the target interval duration; summing the target interval duration and the time data to obtain the target time data.

2. The method according to claim 1, characterized in that Performing a time synchronization operation on the client and the server according to a timer, and determining a time when the time synchronization operation is successfully performed as a second time, includes: Obtaining a minimum display duration for a page displayed by the client; Determine a preset interval duration according to the minimum display duration; The time synchronization operation is performed once on the client and the server at every preset interval according to the timer, and the time when the time synchronization operation is successfully performed is determined as the second time.

3. The method according to claim 2, characterized in that The method further comprises: When the time synchronization operation is performed once on the client and the server at each preset interval according to the timer, detecting whether the time synchronization operation is successfully performed; When the time synchronization operation is not successfully performed, the time synchronization operation is continuously performed on the client and the server according to the timer until the time synchronization operation is successfully performed, and the time when the time synchronization operation is successfully performed is determined as the second time.

4. The method according to claim 3, characterized in that Detecting whether the time synchronization operation is successfully performed includes: In case it is detected that the time synchronization operation is not performed, it is determined that the time synchronization operation is not successfully performed.

5. The method according to claim 3, characterized in that: Detecting whether the time synchronization operation is successfully performed includes: In case that the time synchronization operation fails to be performed, it is determined that the time synchronization operation is not successfully performed.

6. The method according to claim 2, characterized in that After obtaining target time data by calculation according to the first time, the second time and the time data, the method further includes: The target time data is sent to the display page for display.

7. A time synchronization device, characterized in that: include: A sending module is used to send a time acquisition request to the server through the client; A receiving module, configured to receive, through the client, time data generated by the server according to the time acquisition request, and determine the time when the client receives the time data as a first time, wherein the time data is used to represent the length of time that the server has been running when receiving the time acquisition request; a determination module, configured to perform a time synchronization operation on the client and the server according to a timer, and determine a time when the time synchronization operation is successfully performed as a second time, wherein the timer is a timing program deployed in the client; a calculation module, configured to calculate according to the first time, the second time and the time data to obtain target time data, wherein the target time data is used to represent the length of time that the server has been running at the second time; The calculation module includes: a first calculation unit, used to calculate the difference between the second time and the first time to obtain the target interval duration; and a second calculation unit, used to sum the target interval duration and the time data to obtain the target time data.

8. The device according to claim 7, characterized in that The determining module comprises: A first acquisition unit is used to acquire a minimum display time of a page displayed by the client; A first determining unit, configured to determine a preset interval duration according to the minimum display duration; The second determining unit is configured to perform the time synchronization operation on the client and the server once at every preset interval according to the timer, and determine the time when the time synchronization operation is successfully performed as the second time.

9. The device according to claim 8, characterized in that The device also includes: A detection module, configured to detect whether the time synchronization operation is successfully performed when the time synchronization operation is performed once on the client and the server at each preset interval according to the timer; The first determination module is used to continuously perform the time synchronization operation on the client and the server according to the timer when the time synchronization operation is not successfully performed until the time synchronization operation is successfully performed, and determine the time when the time synchronization operation is successfully performed as the second time.

10. The device according to claim 9, characterized in that The detection module comprises: The third determining unit is configured to determine that the time synchronization operation is not successfully performed when it is detected that the time synchronization operation is not performed.

11. The device according to claim 9, characterized in that The detection module comprises: The fourth determining unit is configured to determine that the time synchronization operation is not successfully executed when it is detected that the time synchronization operation fails to be executed.

12. The device according to claim 8, characterized in that The device also includes: The display module is used to send the target time data to the display page for display.

13. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program is configured to execute the time synchronization method described in any one of claims 1 to 6 when running.

14. An electronic device, characterized in that: The electronic device comprises one or more processors; a storage device for storing one or more programs, which, when executed by the one or more processors, enables the one or more processors to run the programs, wherein the programs are configured to execute the time synchronization method described in any one of claims 1 to 6 when run.

Citation Information

Patent Citations

  • Time calibration method and device, computer equipment and storage medium

    CN111224733A

  • Method and system of synchronizing accounting objects between a client and server

    US20110173294A1