Dynamic Fetch server Time

Source: Internet
Author: User

Recently in this project to get to the background of the data, and then use HTML5 to draw the curve, used to be on their own computer use did not find anything unusual, yesterday, the server connected to the colleague found that there is a piece of data is not on the beginning, I thought the program has a problem, Looking for a half-day structure discovery is my computer time and colleague's computer time is not the cause. Now that we have found the problem, we find a solution.

In fact the best solution is to fix the client's time or modify it, but you will find this impossible. So only look for other ways. Then there is the time to get the server and then sync to the client. There are actually three ways to do this game.

1, since it is the dynamic acquisition of a second to obtain once, this can complete the demand but will greatly increase the pressure of the server.

2, get to play once, and then update on the client, this can guarantee that the data obtained at the beginning of the correct, but the late can not guarantee the correctness of the data.

3, at the beginning of the acquisition once, after every minute to get again, this can greatly reduce the pressure on the server, but also to a large extent to ensure the correctness of the data.

Get server time:

/** * Get server Time  */function GetServerTime () {<span style= "white-space:pre" ></span>nowtime = new Date (). GetTime (); <span style= "White-space:pre" ></span>$.ajax (<span style= "White-space:pre" > </span>{<span style= "White-space:pre" ></span>url: ' Servertime_getservertime ', type: ' GET ', DataType: ' JSON ', <span style= "White-space:pre" ></span>success:function (data) <span style= " White-space:pre "></span>{<span style=" White-space:pre "></span>/**<span style=" White-space:pre "></span> * determine if the corresponding key value has corresponding data <span style=" White-space:pre "></span> */<span Style= "White-space:pre" ></span>if (data) <span style= "White-space:pre" ></span>{<span style = "White-space:pre" ></span>nowtime = Data;<span style= "White-space:pre" ></span>}<span style = "White-space:pre" ></span>}<span style= "White-space:pre" &GT;&LT;/SPAN&GT;}); <span style= " White-space:prE "></span>return nowtime;} 

To set up dynamic updates:

function SetTime () {if (timeinterval! = 60000) {TimeInterval + = 1000;nowtime + +;} else{nowtime = GetServerTime (); Timei Nterval = 0;} $ ("#server_time"). HTML ("Server time:" + new Date (nowtime). toString (). substring (15, 24));}

Called when the page is loaded:

<span style= "font-family:arial, Helvetica, Sans-serif;" >window.onload = function () </span>
{nowtime = GetServerTime (); SetInterval (settime, 1000);};

This will enable the dynamic refresh of the time, but not frequently to the background to request data.

Dynamic Fetch server Time

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.