📄 day3_5.html
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312-80">
<style type="text/css">
<!--
a:link { color: blue; text-decoration: none}
a:visited { color: purple; text-decoration: none}
a:hover { color: #CC0033; text-decoration: underline}
-->
</style>
<title>JavaScript高级教程</title>
<script language="JavaScript">
<!-- hide me
function writeTime() {
// get a date object
var today = new Date();
// ask the object for some information
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();
// make the minutes and seconds look right
minutes = fixTime(minutes);
seconds = fixTime(seconds);
// put together the time string and write it out
var the_time = hours + ":" + minutes + ":" + seconds;
window.document.the_form.the_text.value = the_time;
// run this function again in half a second
the_timeout= setTimeout('writeTime();', 500);
}
function fixTime(the_time) {
if (the_time < 10)
{
the_time = "0" + the_time;
}
return the_time;
}
// show me -->
</script>
</head>
<body topmargin="1" leftmargin="2">
<table border="0" width="591" cellspacing="0">
<tr>
<td bgcolor="#ffff99" width="451">JavaScript高级教程 - 第三课</td>
</tr>
<tr>
<td bgcolor="#FF6600" width="451"><a href="mailto:thau@wired.com">Thau</a></td>
</tr>
</table>
<div align="left">
<table border="0" width="630" cellspacing="0">
<tr>
<td width="458" valign="top" align="left" rowspan="2"><small><small><br>
</small></small><strong>第五页:<font size="3" face="宋体">一个Javascript编写的时钟</font></strong>
<p><font size="3"><font face="宋体">现在时间是:</font></font></p>
<font size="3">
<form name="the_form">
<p><font face="宋体">
<input type="text" name="the_text" size="20">
<input type="button" value="启动时钟" onClick="writeTime();">
<input type="button" value="终止时钟" onClick="clearTimeout(the_timeout);">
</font></p>
</form>
<p><font face="宋体">点击“启动时钟”则时钟开始运行。它从你的计算机中读取时间<br>
并每半秒更新一次文字框中的显示。这个例子通过一个自调用<br>
的函数设置了一个定时器。同时这个例子可以让你了解一点Date<br>
对象的功能。当讲解cookies时,我提到过Date对象。 </font></p>
</font>
<p><font size="3"></font><font size="3" face="宋体">以下是代码:<br>
function writeTime() {<br>
<br>
// 获得日期对象<br>
var today = new Date();<br>
<br>
// 从对象中获得信息<br>
var hours = today.getHours();<br>
var minutes = today.getMinutes();<br>
var seconds = today.getSeconds();<br>
<br>
// fixTime 使分和秒可以正常显示<br>
// 对于小于10的数字则在该数字前加一个0<br>
minutes = fixTime(minutes);<br>
seconds = fixTime(seconds);<br>
<br>
//将时间字符串组合在一起并写出<br>
var the_time = hours + ":" + minutes + ":"
+ seconds;<br>
window.document.the_form.the_text.value = the_time;<br>
<br>
//每半秒钟执行一次该函数<br>
the_timeout= setTimeout('writeTime();',500);<br>
<br>
}<br>
<br>
<br>
function fixTime(the_time) {<br>
<br>
<br>
</font><font size="3"><font face="宋体">if (the_time <10) { the_time
= "0" + the_time; } return the_time; } </font></font></p>
<font size="3">
<p><font face="宋体">我们仔细研究一下代码。 </font>
<dl>
<dt><font face="宋体"><tt>var today = new Date();</tt> </font></dt>
<dd><font face="宋体">正如<tt>new Array()</tt> 可以生成一个新的数组,你可以可以<br>
用<tt>new Date()</tt> 生成一个新的日期对象。生成对象之后,<br>
你可以对其提出你的问题。你生成的新的日期对象的括号<br>
中间没有任何参数, 但JavaScript会查询计算机的始终<br>
并用其生成新的日期对象。现在我们的日期对象名为<br>
"today" ,我们可以从中提取相应的信息。 </font></dd>
<dt><font face="宋体"><tt>var hours = today.getHours();</tt> </font></dt>
<dd><font face="宋体">这条用于获得当前的小时值。它是军队格式的时间,即,<br>
如果当前时间是下午两点,则它返回的值是14。<tt>getHours()</tt><br>
是Javascript的日期对象内置的方法调用。 </font></dd>
<dt><font face="宋体"><tt>var minutes = today.getMinutes(); var seconds
= today.getSeconds();</tt> </font></dt>
<dd><font face="宋体">这几行原理和<tt>getHours()类似</tt>。 </font></dd>
<dt><font face="宋体"><tt>minutes = fixTime(minutes);</tt> </font></dt>
<dd><font face="宋体"><tt>getMinutes</tt>存在一些问题,如果分钟是11:01, <tt>getMinutes</tt><br>
将返回 "1"。时钟的显示格式可不是这样,它应该显示为<br>
“01”。<tt>fixTime</tt>函数就是用于执行纠正显示格式的功能。 </font></dd>
<dt><font face="宋体">下面两行将字符串组合在一起并显示出来,</font></dt>
<dt><font face="宋体"><tt>the_timeout = setTimeout('writeTime();', 500);</tt>
</font></dt>
<dd><font face="宋体">设置没半秒执行一次该函数的循环。</font></dd>
</dl>
<p><font face="宋体">下面我们将学习如何在定时器中加入变量。<a href="day3_6.html">>></a></font></p>
</font>
<p><font face="宋体" size="3" color="#000000"><strong>JavaScript高级教程</strong></font><font color="#FF0000" face="宋体" size="3"><br>
</font><font face="宋体"><font color="#FF0000">第一页</font> <a href="day3_5.html">Javascript高级教程
- 第三课</a><br>
<font size="3"><font color="#FF0000">第二页</font> <a href="day3_2.html">如何给事件定时</a><br>
<font color="#FF0000">第三页</font> <a href="day3_3.html">定时循环的概念</a><br>
<font color="#FF0000">第四页</font> <a href="day3_4.html">定时循环的做法</a><br>
<font color="#FF0000">第五页</font> 一个Javascript编写的时钟<br>
<font color="#FF0000">第六页</font> <a href="day3_6.html">给定时器加入变量</a><br>
<font color="#FF0000">第七页</font> <a href="day3_7.html">识别用户的浏览器</a><br>
<font color="#FF0000">第八页</font> <a href="day3_8.html">如何识别用户的浏览器</a><br>
<font color="#FF0000">第九页</font> <a href="day3_9.html">对象和方法的识别</a><br>
<font color="#FF0000">第十页</font> <a href="day3_10.html">History对象</a></font></font></p>
<p>[<a href="day1_1.html">第1课</a>][<a href="day2_1.html">第2课</a>][第3课]<font size="3">[<a href="day4_1.html">第4课</a>][<a href="day5_1.html">第5课</a>]</font></p>
<hr align="left">
<!--webbot bot="Include" U-Include="../../copyright.html" TAG="BODY" startspan -->
<p><font face="verdana, arial, geneva, sans-serif" size="2"><a href="http://phtshop.yeah.net" target="_top">本文根据
网猴 相关文章改编,版权归原作者所有。</a> </font><font color="#000000"><span class="smallfont"></span></font></p>
<!--webbot bot="Include" endspan i-checksum="15926" --> </td>
</tr>
<tr> </tr>
</table>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -