不使用JavaScript的时间花费 [英] Ticking Time Without JavaScript

查看:80
本文介绍了不使用JavaScript的时间花费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里介绍了用于显示动态文本的文字.

所以我用了Literal

Literal.Text = DateTime.Now.ToString();

时间在显示,但没有在滴答滴答.


::我只想使用文字,我知道其他方式::



请帮帮我.

[edit]已删除呼喊声-OriginalGriff [/edit]

I here about literal that it is used to show dynamic text.

So I used Literal

Literal.Text= DateTime.Now.ToString();

Time is showing but it is not ticking.


:: I just want to do it with literal, I know other ways ::



Please Help me.

[edit]SHOUTING removed - OriginalGriff[/edit]

推荐答案

您不能那样做.抱歉,ASP无法那样工作.

当您读取DateTime.Now属性时,您将获得一次使用,一次拍摄,非更新的当前日期和时间的副本.就像.NET中几乎所有其他对象一样,此刻此刻不会改变,除非您专门执行更改其值的代码.

问题是您需要多考虑网页的工作方式. C#通常在服务器上执行-即使您找到了一种将C#在客户端上执行的可能性,C#极有可能根本无法在客户端上执行:除非客户端安装了.NET,否则它将无法工作. >
当用户导航到您的页面时,将启动您的应用程序,并调用页面加载事件,并且在将最终HTML发送给客户端后,您的应用程序可以自由关闭-在繁忙的服务器中,它可能会免费内存.
为了定期从服务器更新客户端显示,您的应用必须始终保持驻留状态.客户端和服务器系统之间的流量也将大大增加.

只需使用Javascript即可.上班很简单!
You can''t do it that way. Sorry, but ASP doesn''t work like that.

When you read the DateTime.Now property, you get a single use, one shot, non-updating copy of the current date and time. Just like nearly all other objects in .NET, it will not change from this moment on, unless you specifically execute code which changes it''s value.

The problem is that you need to think a bit more about how webpages work. C# is normally executed on the server - there is a very good chance that C# cannot execute on the client at all, even if you could find a way to get it there: unless the client has .NET installed it cannot work.

When a use navigates to your page, your application is started, the page load event is called, and when the final HTML has been sent to the client, your app is free to close - and in a busy server, it probably will to free up memory.
In order to update the client display on a regular basis from the server you app would have to remain resident at all times. There would also be a significant increase in traffic between the client and server systems.

Just do it in Javascript. It is simple to get working!


现在,希望您能得到OriginalGriff的解释.

我的答案来了:

您不必使用JavaScript进行操作.只需添加到标题中即可:
Now, I hope you got the explanation by OriginalGriff.

Here comes my answer:

You don''t have to do it in JavaScript. Just add to your header:
<html>
	<head>
		<title>...</title>
		<meta http-equiv="refresh" content="0.7">
	</head>
<body>

<!- ... ->

</body>
</html>

您能理解它有什么区别吗?如果不是,请查找http-equiv和"refresh".

那是关于基于服务器端提供的HTTP响应的解决方案.您可以纯粹在客户端上执行此操作,如OriginalGriff建议的那样.看起来可能像这样:

Can you understand what difference does it make? If not, look for http-equiv and "refresh".

That was about your solution based on the HTTP response provided by the server side. You can do it purely on the client side, like OriginalGriff advised. It can look like this:

<html>
    <head>
        <title>...</title>
        <meta http-equiv="refresh" content="0.7">
    </head>
<body>

<p id="clock">Clock goes here</p>

<script type="text/javascript"><!--
function updateClock () {
  var currentTime = new Date ( );
  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
  document.getElementById("clock").innerHTML = currentTimeString;
}
updateClock ();
--></script>

</body>
</html>



您能理解它的工作原理吗?



实际上,基于完全刷新和"http-equiv"的解决方案非常糟糕.我已经展示了它只是为了说明Web的工作原理,以及服务器端在您的情况下的工作,以作为提示.真正的解决方案应该基于JavaScript计时器.在很多地方都可以找到它.这只是一个:
http://stackoverflow.com/questions/9247977/clock-in-javascript [ ^ ].

—SA



Can you understand how it works?



The solution based on full refresh and "http-equiv" is in fact pretty bad. I''ve shown it just to give an illustration on how Web works, and what server side does in your case, as a hint. A real solution should be based on JavaScript timer. There are many places where you can find it; here is just one:
http://stackoverflow.com/questions/9247977/clock-in-javascript[^].

—SA


我将为您讲一个小故事.

想想挂钟(非数字)的时,分,秒针,每个针都有单独的齿轮.齿轮的职责是在连续完成360度旋转后,将各针移动1步.
因此,每秒更新的时间是由于一个功能(一个步进的针)和具有时间间隔(时间的齿轮旋转)而引起的.

像这样思考(地球,24小时旋转使白天和黑夜),(心跳)等等.....

现在检查您的代码,应用缺少的功能和时间间隔,使时钟保持活动状态.
I am going to give a small story for you.

Think about wall clock(not digital) hour,minute,second needle is there having individual gear for every needle.Responsibility of the gear is to move 1 step of respective needle after a successive completion of a 360 degree rotation.
So, every second, time is getting update is because of a function(stepping needle one step) and having time interval(gear rotation for time), right.

Like that think about (earth, 24hr revolution gives day and night),(heart beat) and so on.....

Now go through your code,apply missing function and time interval and make your clock alive.


这篇关于不使用JavaScript的时间花费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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