⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 day3_3.html

📁 JAVASCRIPT 高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2" height="20"><a href="../../1-backend/cgi_perl/search_engine/index.html">-创建搜索引擎</a></td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2" height="16">-<a href="../../1adobe/GoLive/index.html">Adobe GoLive</a></td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2"><a href="../../1-backend/cgi_perl/templates/index.html">-模板</a></td>
  </tr>
  <tr bgcolor="#666699"> 
    <td colspan="2" align="center"><font color="#FFFFFF">合作伙伴</font></td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2" align="left">-<a href="http://www.5dmedia.com/" target="_blank">5D精英网</a></td>
  </tr>
  <tr align="center" bgcolor="#FFFFFF"> 
    <td colspan="2"> <img src="../../Library/front_monkey.gif" width="59" height="68"></td>
  </tr>
  </tbody> 
</table>
<!-- #EndLibraryItem --> </td>
 <!-- End of headlines (column 1: left column) --> <!-- Gutter between columns 1 and 2 --> 
 <td width="10" height="794"><img src="http://www.sohu.com/images/pixel.gif" width=10></td>
 <!-- Search box and directories (columns 2 and 3: middle columns, combined into one) --> 
 <td align=center valign=top width="558"> 
 <div align="left"><!-- #BeginEditable "1" --> 
<title>JavaScrip高级教程</title>
 <strong>第三页:<font size="3" face="宋体">定时循环的概念</font></strong> 
      <p>其制作方法就是让按钮的onClick事件调用以下函数:<big><br>
        </big><br>
        function ringBell()<br>
        {<br>
                var timer1 = setTimeout(&quot;window.document.the_form.the_text.value='3 
                <br>
                seconds!';&quot;,3000);<br>
                var timer2 = setTimeout(&quot;window.document.the_form.the_text.value='6<br>
                seconds!';&quot;,6000);<br>
                var timer3 = setTimeout(&quot;window.document.the_form.the_text.value='9 
                <br>
                seconds!';&quot;,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(&quot;window.document.the_form.the_text.<br>value='3 seconds!';&quot;,3000);
var timer2 = setTimeout(&quot;window.document.the_form.the_text.<br>value='6 seconds!';&quot;,3000);
var timer3 = setTimeout(&quot;window.document.the_form.the_text.<br>value='9 seconds!';&quot;,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" name="button">
          <input name="the_text2" value="0" size="20">
          <input onClick="clearTimeout(the_timeout);" type="button" value="stop timer" name="button">
        </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(&quot;changeTextBoxTo(2);&quot;,2000);<br>
        var timer2 = setTimeout(&quot;changeTextBoxTo(4);&quot;,4000);<br>
        var timer3 = setTimeout(&quot;changeTextBoxTo(6);&quot;,6000);<br>
        var timer4 = setTimeout(&quot;changeTextBoxTo(8);&quot;,8000);<br>
        var timer5 = setTimeout(&quot;changeTextBoxTo(10);&quot;,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(&quot;changeTextBoxTo(the_tim<br>e);&quot;, the_time*1000);
	}

}
</pre>
      </blockquote>
      <p> </p>
      <p> </p>
      <p><font size="3">请把程序研究一会,看看会得到什么结果。但不要尝试去运<br>
        行它,否则结果会使你很不愉快。让我们在“while&quot;循环中<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 : 所以现在&nbsp; the_time = 2 <br>
            </li>
            <li>var time = setTimeout(&quot;changeTextBoxTo(2);&quot;, 2000) : 
              <br>
              所以, 从现在开始两秒后, 文本框变成了“2.&quot;,这正是我们想要的结果。<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(&quot;changeTextBoxTo(4);&quot;, 4000) : 
              <br>
              所以, 从现在开始四秒后, 文本框变成了“4.&quot;,很好。<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(&quot;changeTextBoxTo(6);&quot;, 6000) : 
              <br>
              所以, 从现在开始六秒后, <br>
              文本框变成了“6.&quot;,好。<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">&gt;&gt;</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>
      <!-- #EndEditable --></div>
 </td>
 <!-- End of search box and directories (columns 2 and 3: middle columns, combined into one) --> 
 <!-- Gutter between columns 3 and 4 --> <!-- Other stuff (column 4: right column) --> 
 <!-- End of other stuff (column 4: right column) --> </tr>
 </table>
<!-- End of table surrounding page contents -->

 <hr noshade size=1 width=700>
 <span class=eng><br>
 Copyright (C) 1998-2000 Internet Technologies China.&nbsp; All rights reserved. 
 </span> 
</center>
</body>
<!-- #EndTemplate --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -