简体   繁体   English

如何获取服务器时间并每秒更新一次?

[英]How to get server time and update every second?

I am making ajax request to a particular server and getting the response also. 我正在向特定服务器发出ajax请求,并且也获得了响应。 I am doing cross domain request so using jsonp. 我正在跨域请求,所以使用jsonp。 Is there a way to get the server time to which i am making the request. 有没有一种方法可以获取我提出请求的服务器时间。 Will it be better to write php script or doing just ajax request is good. 编写php script还是仅执行ajax请求会更好吗? Suppose if i make the following request : 假设我提出以下要求:

$.ajax({
        dataType: 'jsonp',
        data: 'jsonp=date',
        jsonp: 'jsonp_callback',                
        url: 'http://www.google.com',   
        success: function (data) {                  
        }
});

How can i get the server time from this request? 我如何从该请求中获取服务器时间? Please help if any suggestion. 如果有任何建议请帮助。 Also after getting the time if i use setInterval method to update time every second will it be a costly operation or better to make the same ajax request after a particular time to update time. 同样在获取时间之后,如果我使用setInterval方法每秒更新一次时间,那么在特定时间更新时间之后发出相同的ajax请求将是一项成本较高的操作,或者会更好。 I have real time data to update with the time. 我有实时数据,可以随时更新。

You can't get the server's time if it doesn't explicitly provide it to you. 如果未明确提供服务器时间,则无法获得服务器的时间。
You can read its HTTP headers, but that's not a good thing since the headers may not provide this information every time, or their format may not be the same all the time. 您可以读取其HTTP标头,但这不是一件好事,因为标头可能不会每次都提供此信息,或者它们的格式可能一直都不同。

Also, I don't see the point of asking it every second. 另外,我看不出每秒问一次的意义。
Ask it one single time, calculate the difference between its time and yours, and here you go: you got the difference of time and you can use it wherever you want. 一次查询一次,计算出它与您的时间之间的时差,然后就可以开始了:您得到了时差,您可以在任何地方使用它。

Keep in mind that even if you get the time, there probably will be a difference between the one you got and the server's real time because of the network's latency. 请记住,即使您有时间,由于网络的延迟,您获得的时间和服务器的实时时间可能也会有所不同。

The answer to part 1 is that you will need to output the server time in the response in order for your javascript to read it. 第1部分的答案是,您需要在响应中输出服务器时间,以使JavaScript能够读取它。 For part 2, I would wait for the response to load, and then use setTimeout . 对于第2部分,我将等待响应加载,然后使用setTimeout Using setInterval means that you might fire the ajax call twice before the first response returns. 使用setInterval意味着您可能会在第一个响应返回之前触发两次ajax调用。

When you suggest server time do you mean the script that is executing the call or the actual time on the server? 当您建议服务器时间时,是指正在执行调用的脚本还是服务器上的实际时间?

What is the need for this, are you trying to figure out time between calls to automate calls every 5 secs for example? 这有什么需要,例如,您是否要计算出两次通话之间的时间,以每5秒自动进行一次通话?

If you are then just simply getting a locale time from javascript and comparing that from when you get a response in ajax would suffice? 如果您只是简单地从javascript获取语言环境时间并与从ajax中获得响应时的时间进行比较就足够了吗?

Otherwise javascript isn't going to be able to get server time, due to its 'clientside' nature. 否则,由于javascript的“客户端”性质,它将无法获得服务器时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM