﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-AndyZhang-随笔分类-JavaScript</title><link>http://www.blogjava.net/AndyZhang/category/49332.html</link><description>welcome to java world</description><language>zh-cn</language><lastBuildDate>Tue, 09 Aug 2011 08:12:19 GMT</lastBuildDate><pubDate>Tue, 09 Aug 2011 08:12:19 GMT</pubDate><ttl>60</ttl><item><title>JavaScript 计时事件</title><link>http://www.blogjava.net/AndyZhang/archive/2011/08/09/356111.html</link><dc:creator>SkyDream</dc:creator><author>SkyDream</author><pubDate>Tue, 09 Aug 2011 05:41:00 GMT</pubDate><guid>http://www.blogjava.net/AndyZhang/archive/2011/08/09/356111.html</guid><description><![CDATA[<span style="background-color: #f9f9f9; ">
<div style="font-family: Verdana, Arial, Helvetica, sans-serif; width: 607px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 15px; padding-top: 20px; padding-bottom: 20px; border-right-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-top-width: 1px; border-top-style: solid; border-top-color: #aaaaaa; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #aaaaaa; ">
<p style="margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 18px; ">通过使用 JavaScript，我们有能力作到在一个设定的时间间隔之后来执行代码，而不是在函数被调用后立即执行。我们称之为计时事件。</p>
<p style="margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 18px; ">在 JavaScritp 中使用计时事件是很容易的，两个关键方法是:</p>
<dl style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><dt style="margin-top: 15px; margin-right: 0px; margin-bottom: 3px; margin-left: 0px; font-weight: bold; color: #0479a7; ">setTimeout()</dt><dd style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 10px; line-height: 18px; ">未来的某时执行代码</dd><dt style="margin-top: 15px; margin-right: 0px; margin-bottom: 3px; margin-left: 0px; font-weight: bold; color: #0479a7; ">clearTimeout()</dt><dd style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 10px; line-height: 18px; ">取消setTimeout()</dd></dl></div>
<div style="width: 607px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 15px; padding-top: 20px; padding-bottom: 20px; border-right-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-top-width: 1px; border-top-style: solid; border-top-color: #aaaaaa; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #aaaaaa; ">
<h2 style="font-family: Verdana, Arial, Helvetica, sans-serif; ">setTimeout()</h2>
<h3 style="font-family: Verdana, Arial, Helvetica, sans-serif; ">语法</h3>
<pre style="width: 580px; margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; border-style: initial; border-color: initial; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dotted; border-right-style: dotted; border-bottom-style: dotted; border-left-style: dotted; border-top-color: #778855; border-right-color: #778855; border-bottom-color: #778855; border-left-color: #778855; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f5f5f5; font-family: 'Courier New', monospace; font-size: 12px; ">var t=setTimeout("javascript语句",毫秒)</pre>
<p style="font-family: Verdana, Arial, Helvetica, sans-serif; margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 18px; ">setTimeout() 方法会返回某个值。在上面的语句中，值被储存在名为 t 的变量中。假如你希望取消这个 setTimeout()，你可以使用这个变量名来指定它。</p>
<p style="font-family: Verdana, Arial, Helvetica, sans-serif; margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 18px; ">setTimeout() 的第一个参数是含有 JavaScript 语句的字符串。这个语句可能诸如 "alert('5 seconds!')"，或者对函数的调用，诸如 alertMsg()"。</p>
<p style="font-family: Verdana, Arial, Helvetica, sans-serif; margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 18px; ">第二个参数指示从当前起多少毫秒后执行第一个参数。</p>
<p style="font-family: Verdana, Arial, Helvetica, sans-serif; margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 18px; "><span style="font-weight: bold; color: #ff9955; ">提示：</span>1000 毫秒等于一秒。<br />
<span style="font-family: Verdana, Arial, Helvetica, sans-serif; background-color: #f9f9f9; "></span></p>
<h2 style="font-family: Verdana, Arial, Helvetica, sans-serif; ">clearTimeout()</h2>
<h3 style="font-family: Verdana, Arial, Helvetica, sans-serif; ">语法</h3>
<pre style="width: 580px; margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; border-style: initial; border-color: initial; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dotted; border-right-style: dotted; border-bottom-style: dotted; border-left-style: dotted; border-top-color: #778855; border-right-color: #778855; border-bottom-color: #778855; border-left-color: #778855; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; font-family: 'Courier New', monospace; font-size: 12px; background-color: #f5f5f5; ">clearTimeout(setTimeout_variable)
</pre>
<p style="font-family: Verdana, Arial, Helvetica, sans-serif; ">&nbsp;例如：带有停止按钮的无穷循环中的计时事件</p>
<p><div><span style="font-size: 12px;">&lt;html&gt;</span></div><div><span style="font-size: 12px;">&lt;head&gt;</span></div><div><span style="font-size: 12px;">&lt;script type="text/javascript"&gt;</span></div><div><span style="font-size: 12px;">var c=0</span></div><div><span style="font-size: 12px;">var t</span></div><div><span style="font-size: 12px;">function timedCount()</span></div><div><span style="font-size: 12px;">{</span></div><div><span style="font-size: 12px;">document.getElementById('txt').value=c</span></div><div><span style="font-size: 12px;">c=c+1</span></div><div><span style="font-size: 12px;">t=setTimeout("timedCount()",1000)</span></div><div><span style="font-size: 12px;">}</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">function stopCount()</span></div><div><span style="font-size: 12px;">{</span></div><div><span style="font-size: 12px;">clearTimeout(t)</span></div><div><span style="font-size: 12px;">}</span></div><div><span style="font-size: 12px;">&lt;/script&gt;</span></div><div><span style="font-size: 12px;">&lt;/head&gt;</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">&lt;body&gt;</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">&lt;form&gt;</span></div><div><span style="font-size: 12px;">&lt;input type="button" value="开始计时！" onClick="timedCount()"&gt;</span></div><div><span style="font-size: 12px;">&lt;input type="text" id="txt"&gt;</span></div><div><span style="font-size: 12px;">&lt;input type="button" value="停止计时！" onClick="stopCount()"&gt;</span></div><div><span style="font-size: 12px;">&lt;/form&gt;</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">&lt;p&gt;请点击上面的&#8220;开始计时&#8221;按钮来启动计时器。输入框会一直进行计时，从 0 开始。点击&#8220;停止计时&#8221;按钮可以终止计时。&lt;/p&gt;</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">&lt;/body&gt;</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px; ">&lt;/html&gt;</span></div></p>
</div>
</span><img src ="http://www.blogjava.net/AndyZhang/aggbug/356111.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AndyZhang/" target="_blank">SkyDream</a> 2011-08-09 13:41 <a href="http://www.blogjava.net/AndyZhang/archive/2011/08/09/356111.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>