📄 js_timing.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript Timing Events</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>JavaScript Timing Events</h1><a href="js_image_maps.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_objects.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">With JavaScript, it is possible to execute some code NOT
immediately after a function is called, but after a specified time interval.
This is called timing events.</p>
<hr />
<h2>Examples</h2>
<p><a target="_blank" href="tryit.asp@filename=tryjs_timing1">Simple timing</a></p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_timing2">Another simple timing</a></p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_timing_infinite">Timing event in an infinite loop</a></p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_timing_stop">Timing event in an infinite loop - with a Stop button</a></p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_timing_clock">A clock created with a timing event</a></p>
<hr />
<h2>JavaScript Timing Events</h2>
<p>With JavaScript, it is possible to execute some code NOT immediately after a
function is called, but after a specified time interval. This is called timing
events.</p>
<p>It's very easy to time events in JavaScript. The two key methods that are used
are:</p>
<ul>
<li>setTimeout() - executes a code some time in the future</li>
<li>clearTimeout() - cancels the setTimeout()</li>
</ul>
<p><b>Note:</b> The setTimeout() and clearTimeout() are both methods of the HTML
DOM Window object.</p>
<hr />
<h2>setTimeout()</h2>
<h3><b>Syntax</b></h3>
<table class="ex" cellspacing="0" border="1" width="100%" id="table13"><tr><td>
<pre>var t=setTimeout("javascript statement",milliseconds);</pre>
</td></tr></table>
<p>The setTimeout() method returns a value - In the statement above, the value
is stored in a variable called t. If you want to cancel this setTimeout(), you
can refer to it using the variable name.</p>
<p>The first parameter of setTimeout() is a string that contains a JavaScript
statement. This statement could be a statement like "alert('5 seconds!')" or a
call to a function, like "alertMsg()".</p>
<p>The second parameter indicates how many milliseconds from now you want to
execute the first parameter. </p>
<p><b>Note:</b> There are 1000 milliseconds in one second.</p>
<h3>Example</h3>
<p>When the button is clicked in the example below, an alert box will be displayed after 5 seconds.</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table14"><tr><td>
<pre><html>
<head>
<script type="text/javascript">
function timedMsg()
{
var t=setTimeout("alert('5 seconds!')",5000);
}
</script>
</head></pre>
<pre><body>
<form>
<input type="button" value="Display timed alertbox!"
onClick="timedMsg()">
</form>
</body>
</html></pre>
</td></tr></table>
<h3>Example - Infinite Loop</h3>
<p>To get a timer to work in an infinite loop, we must write a function that
calls itself. In the example below, when the button is clicked, the input field
will start to count (for ever), starting at 0:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table16"><tr><td>
<pre><html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout("timedCount()",1000);
}
</script>
</head></pre>
<pre><body>
<form>
<input type="button" value="Start count!"
onClick="timedCount()">
<input type="text" id="txt">
</form>
</body></pre>
<pre></html></pre>
</td></tr></table>
<br />
<hr />
<h2>clearTimeout()</h2>
<h3><b>Syntax</b></h3>
<table class="ex" cellspacing="0" border="1" width="100%" id="table20"><tr><td>
<pre>clearTimeout(setTimeout_variable)</pre>
</td></tr></table>
<h3>Example</h3>
<p>The example below is the same as the "Infinite Loop" example above. The only
difference is that we have now added a "Stop Count!" button that stops the
timer:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table22"><tr><td>
<pre><html>
<head>
<script type="text/javascript">
var c=0
var t</pre>
<pre>function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout("timedCount()",1000);
}</pre>
<pre>function stopCount()
{
clearTimeout(t);
}
</script>
</head></pre>
<pre><body>
<form>
<input type="button" value="Start count!"
onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="Stop count!"
onClick="stopCount()">
</form>
</body></pre>
<pre></html></pre>
</td></tr></table>
<br />
<hr />
<a href="js_image_maps.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_objects.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -