如何使用JavaScript代码在我的网站中显示服务器时钟 [英] how to use javascript code to show server clock in my sites

查看:55
本文介绍了如何使用JavaScript代码在我的网站中显示服务器时钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hellooooooo
我想用javascript显示服务器时钟并使用打击代码,但是它显示时钟,当我刷新页面时它可以工作,请帮帮我
< head>
< script>

hellooooooo
i want to show servers clock with javascript and use blow codes but it show clock and when i refresh the page it work please help me
<head>
<script>

function Time() {

            var hour = parseInt("<%=DateTime.Now.Hour %>", 10);
            var minute = parseInt("<%=DateTime.Now.Minute %>", 10);
            var second = parseInt("<%=DateTime.Now.Second %>", 10);
            document.getElementById("clock").innerHTML = hour.toString() + ":" + minute.toString() + ":" + second.toString();
            setTimeout('Time', 1000);


        setTimeout('Time', 1000);
    }
    </script>
</head>
<body onload="Time();">
    <form id="form1" runat="server">
    <div>
        <div id="clock">
        </div>
        <span id="h"></span><span id="m"></span><span id="S"></span>
    </div>
    </form>
</body>
</html>

推荐答案

这个想法是:出于性能原因,您的时钟应始终在客户端运行.唯一的问题是:JavaScript无法知道"服务器部分的任何内容,因此它不知道"服务器端时区.因此,在ASP.NET中,代码应通过.NET生成时区信息,并将其注入到HTTP响应中生成的JavaScript代码中.

在服务器端,您可以计算相对于UTC的偏移量,并将其转换为毫秒(C#):

The idea is: your clock should work on the client side all the time, for performance reasons. The only problem is: JavaScript cannot "know" anything about the server part, so it is "unaware" of the server-side time zone. So, at ASP.NET code should generate time zone information through .NET and inject it into the JavaScript code generated in HTTP response.

On server side, you can calculate the offset relative to UTC and convert it in milliseconds (C#):

System.TimeZoneInfo timeZone = System.TimeZoneInfo.Local;
System.TimeSpan offset = timeZone.BaseUtcOffset;
double offsetMs = offset.TotalMilliseconds;



您可以通过在JavaScript中生成值来使用偏移量(从脚本的角度来看,它将是一个常量值),并将其用于时间计算中.显示在这里:

http://www.techrepublic. com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329 [ http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.timespan.aspx [ ^ ],
http://www.w3schools.com/jsref/jsref_gettimezoneoffset.asp [



You can use the offset by generating the value in JavaScript (from the script standpoint, it will be a constant value) and use it in time calculations. This is shown here:

http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329[^].

Look at this code, it uses getTimeZoneOffset (minutes) on the client site and convert this value in milliseconds. Don''t do it. Instead, use the value calculated on the server side (through <% %>) instead of utc value shown in the script referenced above.

See also:
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^],
http://www.w3schools.com/jsref/jsref_gettimezoneoffset.asp[^].

—SA


感谢...
我发现它奏效了...

< pre lang ="xml"> var hour = parseInt(& lt;%= DateTime.Now.Hour%& quot ;, 10);
var minutes = parseInt(&%= DateTime.Now.Minute%&",10);
var second = parseInt(&%= DateTime.Now.Second%&",10);
//var x = hour.tostring()+&:&" + minutes.tostring()+& quot ::&"+ second.tostring();
//alert(x);

函数showTime(){
second ++;
if(second& gt; 59){
秒= 0;
分钟++;
}

如果(分钟& gt; 59){
分钟= 0;
小时++;
}
分钟= checkTime(分钟);
秒= checkTime(秒);
var clock = hour.toString()+& quot ::& quot; + minutes.toString()+& quot ::& quot; + second.toString();
document.getElementById(& quot; clock& quot;).innerHTML = clock;
函数checkTime(i){
如果(i& lt; 10){
i =&"0" + i;
}
返回我;
}
}

setInterval(''showTime()'',1000);

& lt//script& gt;
& lt//head& gt;
& lt; body& gt;
& lt; form id =& quot; form1& quot; runat =& quot;服务器& gt;
& lt; br/& gt;
& lt; br/& gt;
& lt; br/& gt;
& lt; br/& gt;
& lt; div class =& quot; divClock& quot; id =&"clock&">
& lt//div& gt;
& lt//form& gt;
& lt//body& gt;
& lt;/html</pre>
& gt;
thanks...
i found it itis worked...

<pre lang="xml">var hour = parseInt(&quot;&lt;%=DateTime.Now.Hour %&gt;&quot;, 10);
var minute = parseInt(&quot;&lt;%=DateTime.Now.Minute %&gt;&quot;, 10);
var second = parseInt(&quot;&lt;%=DateTime.Now.Second %&gt;&quot;, 10);
// var x = hour.tostring() +&quot;:&quot;+ minute.tostring() +&quot;:&quot;+ second.tostring();
// alert(x);

function showTime() {
second++;
if (second &gt; 59) {
second = 0;
minute++;
}

if (minute &gt; 59) {
minute = 0;
hour++;
}
minute = checkTime(minute);
second = checkTime(second);
var clock = hour.toString() + &quot;:&quot; + minute.toString() + &quot;:&quot; + second.toString();
document.getElementById(&quot;clock&quot;).innerHTML = clock;
function checkTime(i) {
if (i &lt; 10) {
i = &quot;0&quot; + i;
}
return i;
}
}

setInterval(''showTime()'', 1000);

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;divClock&quot; id=&quot;clock&quot;&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html</pre>
&gt;


这篇关于如何使用JavaScript代码在我的网站中显示服务器时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆