{"id":43,"date":"2007-07-25T16:13:16","date_gmt":"2007-07-25T20:13:16","guid":{"rendered":"http:\/\/prglab.com\/blog\/js-tutorial\/js-timing"},"modified":"2007-07-25T16:40:59","modified_gmt":"2007-07-25T20:40:59","slug":"js-timing","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/js-tutorial\/js-timing","title":{"rendered":"JavaScript \u7684\u8ba1\u65f6\u4e8b\u4ef6"},"content":{"rendered":"<p>\u4f7f\u7528JavaScript\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u5ef6\u65f6\u6267\u884c\uff0c\u4e5f\u5c31\u662f\u8bf4\u5f53\u4e00\u4e2a\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u4e0d\u7acb\u5373\u6267\u884c\u67d0\u4e9b\u4ee3\u7801\uff0c\u800c\u662f\u7b49\u4e00\u6bb5\u6307\u5b9a\u7684\u65f6\u95f4\u540e\u518d\u6267\u884c\uff0c\u8fd9\u5c31\u53eb\u505a\u8ba1\u65f6\u4e8b\u4ef6\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<h2>JavaScript \u7684\u8ba1\u65f6\u4e8b\u4ef6<\/h2>\n<p>JavaScript\u7684\u8ba1\u65f6\u4e8b\u4ef6\u5f88\u5bb9\u6613\u4f7f\u7528\uff0c\u4e24\u4e2a\u5173\u952e\u7684\u51fd\u6570\u662f\uff1a<\/p>\n<ul>\n<li>setTimeout() &#8211; \u5728\u6307\u5b9a\u65f6\u95f4\u540e\u6267\u884c\u4ee3\u7801<\/li>\n<li>clearTimeout() &#8211; \u53d6\u6d88 setTimeout()<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f:<\/strong> setTimeout() \u548c clearTimeout() \u90fd\u662fHTML  DOM \u7684 Window \u5bf9\u8c61\u7684\u51fd\u6570\u3002<\/p>\n<hr \/>\n<h2>setTimeout()<\/h2>\n<h3><strong>\u8bed\u6cd5<\/strong><\/h3>\n<table class=\"ex\" id=\"table13\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\n<pre>var t=setTimeout(\"javascript statement\",milliseconds)<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>\u51fd\u6570 setTimeout() \u8fd4\u56de\u4e00\u4e2a\u503c &#8211; \u5728\u4e0a\u9762\u8fd9\u4e2a\u8bed\u53e5\u4e2d\uff0c\u8fd4\u56de\u503c\u88ab\u5b58\u50a8\u5728\u4e86\u53d8\u91cft\u4e2d\uff0c\u4f5c\u4e3a\u6307\u5411\u8fd9\u4e2asetTimeout()\u7684\u6307\u9488\uff0c\u4ee5\u5907\u8981\u53d6\u6d88\u8fd9\u4e2a setTimeout()\u6216\u5f15\u7528\u5b83\u65f6\u4f7f\u7528\u3002<\/p>\n<p>\u51fd\u6570setTimeout()\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5b58\u50a8\u7684\u662f\u4e00\u4e2aJavaScript\u8bed\u53e5\uff0c\u8fd9\u4e2a\u8bed\u53e5\u53ef\u4ee5\u53ea\u662f\u4e00\u4e2a\u547d\u4ee4\uff0c\u6bd4\u5982 &#8220;alert(&#8216;5 seconds!&#8217;)&#8221; \uff0c\u4e5f\u53ef\u4ee5\u4f7f\u8c03\u7528\u67d0\u4e2a\u51fd\u6570\uff0c\u4f8b\u5982 &#8220;alertMsg()&#8221;.\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u4e86\u4ece\u73b0\u5728\u5f00\u59cb\u7b49\u591a\u5c11\u6beb\u79d2\u518d\u6267\u884c\u8fd9\u4e2a\u8bed\u53e5\u3002<\/p>\n<p><strong>\u6ce8\u610f:<\/strong> 1000\u6beb\u79d2 = 1\u79d2\u3002<\/p>\n<h3>\u4f8b\u5b50<\/h3>\n<p>\u4e0b\u9762\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u540e3\u79d2\u4f1a\u663e\u793a\u4e00\u4e2a\u63d0\u793a\u7a97\u53e3\u3002<\/p>\n<table class=\"ex\" id=\"table14\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\n<pre>&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;script type=\"text\/javascript\"&gt;\r\nfunction timedMsg()\r\n{\r\nvar t=setTimeout(\"alert('3 seconds!')\",3000)\r\n}\r\n&lt;\/script&gt;\r\n&lt;\/head&gt;<\/pre>\n<pre>&lt;body&gt;\r\n&lt;form&gt;\r\n&lt;input type=\"button\" value=\"\u663e\u793a\u4e00\u4e2a\u5ef6\u8fdf\u7684\u63d0\u793a\u7a97!\"\r\nonClick=\"timedMsg()\"&gt;\r\n&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>\u6548\u679c\u6f14\u793a\uff1a<br \/>\n<script type=\"text\/javascript\"> function timedMsg() { var t=setTimeout(\"alert('3 \u79d2!')\",3000); } <\/script><\/p>\n<form> <input value=\"\u663e\u793a\u4e00\u4e2a\u5ef6\u8fdf\u7684\u63d0\u793a\u7a97!\" onclick=\"timedMsg()\" type=\"button\" \/> <\/form>\n<p><strong>\u4f8b\u5b50 &#8211; \u65e0\u9650\u5faa\u73af<\/strong><\/p>\n<p>\u8981\u5728\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u4e2d\u4f7f\u7528\u8ba1\u65f6\u5668\uff0c\u6211\u4eec\u9700\u8981\u5199\u4e00\u4e2a\u9012\u5f52\u8c03\u7528\u7684\u51fd\u6570\uff08\u4e00\u4e2a\u4f1a\u8c03\u7528\u81ea\u5df1\u7684\u51fd\u6570\uff09\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u540e\uff0c\u8f93\u5165\u6846\u4f1a\u4ece\uff10\u5f00\u59cb\u8ba1\u6570\uff0c\u5e76\u6c38\u8fdc\u8ba1\u4e0b\u53bb\uff1a<\/p>\n<table class=\"ex\" id=\"table16\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\n<pre>&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;script type=\"text\/javascript\"&gt;\r\nvar c=0\r\nvar t\r\nfunction timedCount()\r\n{\r\ndocument.getElementById('txt').value=c\r\nc=c+1\r\nt=setTimeout(\"timedCount()\",1000)\r\n}\r\n&lt;\/script&gt;\r\n&lt;\/head&gt;<\/pre>\n<pre>&lt;body&gt;\r\n&lt;form&gt;\r\n&lt;input type=\"button\" value=\"\u5f00\u59cb\u8ba1\u6570!\"\r\nonClick=\"timedCount()\"&gt;\r\n&lt;input type=\"text\" id=\"txt\"&gt;\r\n&lt;\/form&gt;\r\n&lt;\/body&gt;<\/pre>\n<pre>&lt;\/html&gt;<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>\u6548\u679c\u6f14\u793a\uff1a<br \/>\n<script type=\"text\/javascript\">  var c=0;  var t;  function timedCount() {  document.getElementById(\"txt\").value=c;  c=c+1;  t=setTimeout(\"timedCount()\",1000); } <\/script><\/p>\n<form> <input value=\"\u5f00\u59cb\u8ba1\u6570!\" onclick=\"timedCount()\" type=\"button\" \/> <input id=\"txt\" type=\"text\" \/> <\/form>\n<hr \/>\n<h2>clearTimeout()<\/h2>\n<h3><strong>\u8bed\u6cd5<\/strong><\/h3>\n<table class=\"ex\" id=\"table20\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\n<pre>clearTimeout(setTimeout_variable)<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<h3>\u4f8b\u5b50<\/h3>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u540c\u6837\u4f7f\u7528\u4e0a\u9762\u201c\u65e0\u9650\u5faa\u73af\u201d\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u4e0d\u540c\u662f\u6211\u4eec\u73b0\u5728\u589e\u52a0\u4e86\u4e00\u4e2a\u505c\u6b62\u8ba1\u6570\u7684\u6309\u94ae\uff1a<\/p>\n<table class=\"ex\" id=\"table22\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\n<pre>&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;script type=\"text\/javascript\"&gt;\r\nvar c=0\r\nvar t<\/pre>\n<pre>function timedCount()\r\n{\r\ndocument.getElementById('txt').value=c\r\nc=c+1\r\nt=setTimeout(\"timedCount()\",1000)\r\n}<\/pre>\n<pre>function stopCount()\r\n{\r\nclearTimeout(t)\r\n}\r\n&lt;\/script&gt;\r\n&lt;\/head&gt;<\/pre>\n<pre>&lt;body&gt;\r\n&lt;form&gt;\r\n&lt;input type=\"button\" value=\"\u5f00\u59cb\u8ba1\u6570!\"\r\nonClick=\"timedCount()\"&gt;\r\n&lt;input type=\"text\" id=\"txt\"&gt;\r\n&lt;input type=\"button\" value=\"\u505c\u6b62\u8ba1\u6570!\"\r\nonClick=\"stopCount()\"&gt;\r\n&lt;\/form&gt;\r\n&lt;\/body&gt;<\/pre>\n<pre>&lt;\/html&gt;<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>\u6548\u679c\u6f14\u793a\uff1a<br \/>\n<script type=\"text\/javascript\">  var c1=0; var t1; function timedCount1() {  document.getElementById(\"txt1\").value=c1;  c1=c1+1;  t1=setTimeout(\"timedCount1()\",1000);  } function stopCount() { clearTimeout(t1); } <\/script><\/p>\n<form> <input value=\"\u5f00\u59cb\u8ba1\u6570!\" onclick=\"timedCount1()\" type=\"button\" \/> <input id=\"txt1\" type=\"text\" \/> <input value=\"\u505c\u6b62\u8ba1\u6570!\" onclick=\"stopCount()\" type=\"button\" \/> <\/form>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528JavaScript\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u5ef6\u65f6\u6267\u884c\uff0c\u4e5f\u5c31\u662f\u8bf4\u5f53\u4e00\u4e2a\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u4e0d\u7acb\u5373\u6267\u884c\u67d0\u4e9b\u4ee3\u7801\uff0c\u800c\u662f\u7b49\u4e00\u6bb5\u6307\u5b9a\u7684\u65f6\u95f4\u540e\u518d\u6267\u884c\uff0c\u8fd9\u5c31\u53eb\u505a\u8ba1\u65f6\u4e8b\u4ef6\u3002 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; JavaScript \u7684\u8ba1\u65f6\u4e8b\u4ef6 JavaScript\u7684\u8ba1\u65f6\u4e8b\u4ef6\u5f88\u5bb9\u6613\u4f7f\u7528\uff0c\u4e24\u4e2a\u5173\u952e\u7684\u51fd\u6570\u662f\uff1a setTimeout() &#8211; \u5728\u6307\u5b9a\u65f6\u95f4\u540e\u6267\u884c\u4ee3\u7801 clearTimeout() &#8211; \u53d6\u6d88 setTimeout() \u6ce8\u610f: setTimeout() \u548c clearTimeout() \u90fd\u662fHTML DOM \u7684 Window \u5bf9\u8c61\u7684\u51fd\u6570\u3002 setTimeout() \u8bed\u6cd5 var t=setTimeout(&#8220;javascript statement&#8221;,milliseconds) \u51fd\u6570 setTimeout() \u8fd4\u56de\u4e00\u4e2a\u503c &#8211; \u5728\u4e0a\u9762\u8fd9\u4e2a\u8bed\u53e5\u4e2d\uff0c\u8fd4\u56de\u503c\u88ab\u5b58\u50a8\u5728\u4e86\u53d8\u91cft\u4e2d\uff0c\u4f5c\u4e3a\u6307\u5411\u8fd9\u4e2asetTimeout()\u7684\u6307\u9488\uff0c\u4ee5\u5907\u8981\u53d6\u6d88\u8fd9\u4e2a setTimeout()\u6216\u5f15\u7528\u5b83\u65f6\u4f7f\u7528\u3002 \u51fd\u6570setTimeout()\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5b58\u50a8\u7684\u662f\u4e00\u4e2aJavaScript\u8bed\u53e5\uff0c\u8fd9\u4e2a\u8bed\u53e5\u53ef\u4ee5\u53ea\u662f\u4e00\u4e2a\u547d\u4ee4\uff0c\u6bd4\u5982 &#8220;alert(&#8216;5 seconds!&#8217;)&#8221; \uff0c\u4e5f\u53ef\u4ee5\u4f7f\u8c03\u7528\u67d0\u4e2a\u51fd\u6570\uff0c\u4f8b\u5982 &#8220;alertMsg()&#8221;.\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u4e86\u4ece\u73b0\u5728\u5f00\u59cb\u7b49\u591a\u5c11\u6beb\u79d2\u518d\u6267\u884c\u8fd9\u4e2a\u8bed\u53e5\u3002 \u6ce8\u610f: 1000\u6beb\u79d2 = 1\u79d2\u3002 \u4f8b\u5b50 \u4e0b\u9762\u4f8b\u5b50\u4e2d\uff0c\u5f53\u6309\u94ae\u88ab\u70b9\u51fb\u540e3\u79d2\u4f1a\u663e\u793a\u4e00\u4e2a\u63d0\u793a\u7a97\u53e3\u3002 &lt;html&gt; &lt;head&gt; &lt;script type=&#8221;text\/javascript&#8221;&gt; function timedMsg() { var t=setTimeout(&#8220;alert(&#8216;3 seconds!&#8217;)&#8221;,3000) } &lt;\/script&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3,"menu_order":30,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/43"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":0,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/43\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/3"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}