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

📄 day3_6.html

📁 Javascript高级教程,可以对深入了解Javascript有所帮助
💻 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>
</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><font size="3"><font face="宋体">定时器的功能主要在于设定某个未来的时间,让Javascript到<br>
          时执行某个语句:<br>
          <br>
          var the_string = &quot;hello&quot;;<br>
          the_timeout = setTimeout(&quot;alert(the_string);&quot;, 60000);<br>
          <br>
          这两行指令让JavaScript等待一分钟然后弹出一个提示框显<br>
          示<tt>the_string</tt>变量所包含的内容。所以一分钟后JavaScript就<br>
          寻找叫做<tt>the_string</tt>的变量并调用<tt>alert()</tt>。但是问题是一分钟<br>
          后该变量中所包含的内容可能后是别的什么。所以如果你把这<br>
          两行代码放在一个函数内,<tt>setTimeout</tt>有可能会报错。例如在<br>
          下例中:<br>
          function alertInAMinute()<br>
          {<br>
          var the_string = &quot;hello&quot;;<br>
          the_timeout = setTimeout(&quot;alert(the_string);&quot;, 60000);<br>
          }<br>
          </font></font></p>
        <font size="3"> 
        <p><font face="宋体">然后你在某个链接中调用这个函数:</font></p>
        </font>
        <blockquote>
          <pre>
      <font size="3"></font><font size="3" face="宋体">
&lt;a href=&quot;#&quot; onClick=&quot;alertInAMinute(); return false;&quot;&gt;blah!&lt;/a&gt;
</font></pre>
        </blockquote>
        <font size="3"> 
        <p><font face="宋体">可能会出错。因为你定义名为<tt>the_string</tt>的变量时用的是<tt>var</tt>,<br>
          而当你在一个函数内使用<tt>var</tt>时,Javascript将其理解为该变量<br>
          只存在于该函数中。一旦离开该函数而进入另一个函数后,该<br>
          变量就会从Javascript的记忆中消失。所以当后来需要调用该变<br>
          量时,Javascript从它的记忆中是不可能找到该变量的,错误因<br>
          此就产生了。 </font></p>
        <p><font face="宋体">产生问题的原因在于你将一个变量传递给<tt>setTimeout</tt>。你可以通<br>
          过将变量的值而不是变量本身传递给setTime来避免这个问题: </font></p>
        <blockquote> 
          <pre><font face="宋体">function alertInAMinute()
{
	var the_string = &quot;hello&quot;;
	the_timeout = setTimeout(&quot;alert(&quot; + the_string + &quot;);&quot;,60000);
}
</font></pre>
        </blockquote>
        <p><font face="宋体">这段代码将变量<tt>the_string</tt>从<tt>setTimeout</tt>的引号中拉出来,由于<br>
          该变量变量目前不在setTimeout的引号内,JavaScript就可以从<br>
          记忆中找到该变量的值。 </font></p>
        <p><font face="宋体">定时器在动态HTML中用途非常广泛,所以值得你仔细研究它。</font></p>
        <p><font face="宋体">要使所有的拥护都能看到动态HTML的内容,你必须了解如何识别<br>
          用户所使用的浏览器。<a href="day3_7.html">&gt;&gt;</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_6.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> <a href="day3_5.html">一个Javascript编写的时钟</a><br>
          <font color="#FF0000">第六页</font> 给定时器加入变量<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 + -