📄 day3_3.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
var the_count = 0;
var the_timeout;
function doTimer()
{
window.document.timer_form.the_text.value = the_count;
the_count += 2;
the_timeout = setTimeout("doTimer();", 2000);
}
function doDumbTimer()
{
var timer1 = setTimeout("window.document.the_form.the_text.value='3 seconds!';", 3000);
var timer2 = setTimeout("window.document.the_form.the_text.value='6 seconds!';", 3000);
var timer3 = setTimeout("window.document.the_form.the_text.value='9 seconds!';", 3000);
}
// 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="宋体">定时循环的概念</font></strong>
<p>其制作方法就是让按钮的onClick事件调用以下函数:<big><br>
</big><br>
function ringBell()<br>
{<br>
var timer1 = setTimeout("window.document.the_form.the_text.value='3
seconds!';",3000);<br>
var timer2 = setTimeout("window.document.the_form.the_text.value='6
seconds!';",6000);<br>
var timer3 = setTimeout("window.document.the_form.the_text.value='9
seconds!';",9000);<br>
<br>
}<big><br>
<br>
<br>
</big><font size="3">它的意思是,“从现在开始,三秒钟后显示‘三秒’,六秒钟<br>
后显示‘六秒’,九秒钟后显示‘九秒’”,很好理解,对吧?</font></p>
<font size="3">
<p>但是,下面这样却不行:</p>
</font>
<blockquote>
<pre>function doDumbTimer()
{var timer1 = setTimeout("window.document.the_form.the_text.value='3 seconds!';",3000);
var timer2 = setTimeout("window.document.the_form.the_text.value='6 seconds!';",3000);
var timer3 = setTimeout("window.document.the_form.the_text.value='9 seconds!';",3000);
}
</pre>
</blockquote>
<form name="the_form">
<p><big>
<input name="the_text" size="20">
</big></p>
</form>
<p><font size="3">试一下这个错误的定时代码看看会发生什么?<br>
<a href="backup/day3_3.htm#" onclick="doDumbTimer(); return false;">faulty
timer code</a>? </font></p>
<font size="3">
<p>请注意当你等了三秒钟,三个定时信息之一神秘地出现在文<br>
本框里,然后就停在那儿。在上面的不好的代码中,每个<br>
<tt>setTimeout</tt>都连续地执行,(就是“从现在开始,三秒钟<br>
后显示‘三秒’,三秒钟后显示‘六秒’,三秒钟后显示<br>
‘九秒’”)。所以当三秒钟以后,三件事儿都发生了,你<br>
得到的正好是其中最后发生的结果----当然不是你希望的<br>
结果。</p>
<p>一旦你理解了,<tt>setTimeout()</tt>还是相当容易使用的。但是<br>
一个难点儿的问题提出来了:你如何去做一个定时器,让某<br>
件事每隔2秒钟就发生一次,从现在一直到永远?象这个例<br>
子:<br>
</p>
<form name="timer_form">
<p>
<input onclick="doTimer();" type="button" value="start timer">
<input name="the_text" value="0" size="20">
<input onclick="clearTimeout(the_timeout);" type="button" value="stop timer">
</p>
</form>
<p>先别担心停止定时器按钮,稍后我会讲<tt>clearTimeouts</tt>。只<br>
要想想你怎么能够让定时器无限循环。实际上这是一个非常<br>
重要的问题而不仅仅是一个小练习。就象我前边提到的那样,<br>
当你用动态HTML让什么东西缓缓地在屏幕上移动时,就执行<br>
一个定时循环:“轻轻移动一点,等待,再移动一点,再等<br>
待.....如此这般”<br>
<br>
你想到这个问题了吗?</p>
<p>好,答案并非那么简单。你无法象上面的那个例子那样,只<br>
用一个函数,就能够每隔两秒就改变文本框的内容,象这样:<br>
<br>
function theTimer()<br>
{<br>
var timer1 = setTimeout("changeTextBoxTo(2);",2000);<br>
var timer2 = setTimeout("changeTextBoxTo(4);",4000);<br>
var timer3 = setTimeout("changeTextBoxTo(6);",6000);<br>
var timer4 = setTimeout("changeTextBoxTo(8);",8000);<br>
var timer5 = setTimeout("changeTextBoxTo(10);",10000);<br>
.<br>
.<br>
.<br>
<br>
}<br>
因为,好,你可以看出为什么不行:如果你想用这种方法让<br>
某件事无限循环下去,你必须有无限多行的代码。相比起其<br>
它问题,比如敲得你肩酸背痛来说,光是下载一个包含了无<br>
限多行javascript的页面就需要太长的时间,所以,这种方<br>
法根本就谈不上是一种选择。<br>
<br>
这个也不行,虽然它看起来更酷一些:</p>
</font>
<p> </p>
<blockquote>
<pre><big>
</big>function theTimer()
{
the_time = 0;
hellIsHot = true;
while (hellIsHot == true)
{
the_time += 2;
var timer = setTimeout("changeTextBoxTo(the_time);", the_time*1000);
}
}
</pre>
</blockquote>
<p> </p>
<p> </p>
<p><font size="3">请把程序研究一会,看看会得到什么结果。但不要尝试去运<br>
行它,否则结果会使你很不愉快。让我们在“while"循环中<br>
走几趟:</font>
<dl><font size="3">
<dt>第一遍</dt>
</font>
<dd>
<ul>
<li> <font size="3">while (hellIsHot == true) : 是的地狱还<br>
是热的。</font><big><br>
</big></li>
<font size="3">
<li>the_time += 2 : 所以现在 the_time = 2 <br>
</li>
<li>var time = setTimeout("changeTextBoxTo(2);", 2000)
: <br>
所以, 从现在开始两秒后, 文本框变成了“2.",这正是我们想要的结果。<br>
</li>
</font>
</ul>
</dd>
<font size="3">
<dt>第二遍</dt>
<dd>
<ul>
<li>while (hellIsHot == true) : 确实,地狱还是热的. <br>
. </li>
<li>the_time += 2 : 所以现在 the_time = 4 <br>
</li>
<li>var time = setTimeout("changeTextBoxTo(4);", 4000)
: <br>
所以, 从现在开始四秒后, 文本框变成了“4.",很好。<br>
</li>
</ul>
</dd>
<dt>第三遍</dt>
<dd>
<ul>
<li>while (hellIsHot == true) : 不, 地狱一<br>
点也没凉快. <br>
</li>
<li>the_time += 2 : 所以现在 the_time = 6 <br>
</li>
<li>var time = setTimeout("changeTextBoxTo(6);", 6000)
: <br>
所以, 从现在开始六秒后, <br>
文本框变成了“6.",好。<br>
</li>
</ul>
</dd>
<dt>第四遍</dt>
<dd>
<ul>
<li>while (hellIsHot == true) : 是的,还是<br>
热的。<br>
</li>
<li>还那样 </li>
<li>还那样 </li>
</ul>
</dd>
</font></dl>
<p> <font size="3">你看明白了。这段代码看起来象是做对了。不幸的是其实不<br>
是这样。相反它创建了一个死循环,一直设定<tt>setTimeouts</tt><br>
直到地狱冷下来。这里有两个问题。首先,当在循环里时,<br>
你的浏览器就无法做任何其它的事情,基本上就是停止,执<br>
行动作,再设定下一个定时器,一直到永远。第二,每次设<br>
定<tt>setTimeout</tt></font><tt><font size="4">时,浏览器</font></tt><font size="3">
都要记住你预定执行的内容以及<br>
何时执行。最终你的浏览器会把内存耗尽,这时你的浏览器<br>
会崩溃,或者你的计算机会崩溃,<font face="宋体">或者把你弄疯而永远也不<br>
想再写一行Javascript程序了。</font></font></p>
<font size="3">
<p>一点都不好</p>
<p>幸运的是,有一种方法能够写出成功的定时器循环。</p>
</font>
<p><a href="day3_4.html">>></a></p>
<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_3.html">Javascript高级教程
- 第三课</a><br>
<font size="3"><font color="#FF0000">第二页</font> <a href="day3_2.html">如何给事件定时</a><br>
<font color="#FF0000">第三页</font> 定时循环的概念<br>
<font color="#FF0000">第四页</font> <a href="day3_4.html">定时循环的做法</a><br>
<font color="#FF0000">第五页</font> <a href="day3_5.html">一个Javascript编写的时钟</a><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><font size="3">[<a href="day1_1.html">第1课</a>][<a href="day2_1.html">第2课</a>][第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 + -