{"id":27,"date":"2005-10-28T21:03:03","date_gmt":"2005-10-29T02:03:03","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/?p=27"},"modified":"2009-07-20T14:38:54","modified_gmt":"2009-07-20T21:38:54","slug":"put-javascript-to-sleep","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2005\/10\/28\/put-javascript-to-sleep\/","title":{"rendered":"Put JavaScript To Sleep"},"content":{"rendered":"<p>Every Java programmer will tell you that you can pause a program using the sleep static method of the Thread class.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nThread.sleep(milliseconds);\r\n<\/pre>\n<p>JavaScript does not have a wait or sleep method but instead provides the setTimeout and setInterval functions.  Here is some JavaScript code to demonstrate the setTimeout function:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nsetTimeout('alert(&quot;Hello, World&quot;)', 4000);\r\nalert('Hello, Web');\r\n<\/pre>\n<p>The Hello, World message will display after 4 seconds, and after the Hello, Web message.  In essence, this function treats a piece of JavaScript code as a child and gives it a &#8216;time out.&#8217;  In contrast, the setInterval method will repeatedly execute a piece of code at a given time interval in milliseconds until you stop it.  When compare side by side, I rather use the setTimeout function.  Here is a recursive-like example using the setTimeout function that mimics setInterval.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar myColors = new Array(\r\n    '#000000',\r\n    '#FF0000',\r\n    '#00FF00',\r\n    '#0000FF',\r\n    '#FFFFFF'\r\n    );\r\n\r\nfunction myFunx(index) {\r\n    if(!index || index &gt; myColors.length) {\r\n        index = 0;\r\n    }\r\n    setTimeout('myFunx('+(index+1)+')', 2000);\r\n    document.bgColor=myColors&#x5B;index];\r\n}\r\n\r\nmyFunx();\r\n<\/pre>\n<p>The example above updates the background color every other second.  Don&#8217;t try this at home unless you are a trained professional or are writing trivial JavaScript examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every Java programmer will tell you that you can pause a program using the sleep static method of the Thread class. Thread.sleep(milliseconds); JavaScript does not have a wait or sleep method but instead provides the setTimeout and setInterval functions. Here is some JavaScript code to demonstrate the setTimeout function: setTimeout(&#8216;alert(&quot;Hello, World&quot;)&#8217;, 4000); alert(&#8216;Hello, Web&#8217;); The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,16,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-r","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/27"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":3,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":773,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/27\/revisions\/773"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}