{"id":344,"date":"2008-03-24T20:42:36","date_gmt":"2008-03-25T01:42:36","guid":{"rendered":"http:\/\/www.phpied.com\/sleep-in-javascript\/"},"modified":"2008-03-24T20:43:12","modified_gmt":"2008-03-25T01:43:12","slug":"sleep-in-javascript","status":"publish","type":"post","link":"https:\/\/www.phpied.com\/sleep-in-javascript\/","title":{"rendered":"sleep() in JavaScript"},"content":{"rendered":"<p>PHP has a <a href=\"http:\/\/php.net\/sleep\">sleep()<\/a> function, but JavaScript doesn't. Well, this is because it's useless, you might say, and you'll be right. But for simulating heavy processing and for misc performance measurements, it could be useful. So here's how you can go about creating a sleep() in JavaScript.<\/p>\n<h3>The code<\/h3>\n<div class=\"hl-main\">\n<pre><span class=\"hl-reserved\">function<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-identifier\">sleep<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-identifier\">milliseconds<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-brackets\">{<\/span><span class=\"hl-code\">\r\n  <\/span><span class=\"hl-reserved\">var<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-identifier\">start<\/span><span class=\"hl-code\"> = <\/span><span class=\"hl-reserved\">new<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-builtin\">Date<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">getTime<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">;\r\n  <\/span><span class=\"hl-reserved\">for<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-reserved\">var<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-identifier\">i<\/span><span class=\"hl-code\"> = <\/span><span class=\"hl-number\">0<\/span><span class=\"hl-code\">; <\/span><span class=\"hl-identifier\">i<\/span><span class=\"hl-code\"> &lt; <\/span><span class=\"hl-number\">1<\/span><span class=\"hl-identifier\">e7<\/span><span class=\"hl-code\">; <\/span><span class=\"hl-identifier\">i<\/span><span class=\"hl-code\">++<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-brackets\">{<\/span><span class=\"hl-code\">\r\n    <\/span><span class=\"hl-reserved\">if<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-reserved\">new<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-builtin\">Date<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">getTime<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\"> - <\/span><span class=\"hl-identifier\">start<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\"> &gt; <\/span><span class=\"hl-identifier\">milliseconds<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-brackets\">{<\/span><span class=\"hl-code\">\r\n      <\/span><span class=\"hl-reserved\">break<\/span><span class=\"hl-code\">;\r\n    <\/span><span class=\"hl-brackets\">}<\/span><span class=\"hl-code\">\r\n  <\/span><span class=\"hl-brackets\">}<\/span><span class=\"hl-code\">\r\n<\/span><span class=\"hl-brackets\">}<\/span><\/pre>\n<\/div>\n<h3>Usage<\/h3>\n<p>Sleep for 1 second (1000 milliseconds):<\/p>\n<div class=\"hl-main\">\n<pre><span class=\"hl-identifier\">console<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">log<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-reserved\">new<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-builtin\">Date<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">;\r\n<\/span><span class=\"hl-identifier\">console<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">log<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-string\">Dude!<\/span><span class=\"hl-quotes\">'<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">;\r\n<\/span><span class=\"hl-identifier\">sleep<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-number\">1000<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">;\r\n<\/span><span class=\"hl-identifier\">console<\/span><span class=\"hl-code\">.<\/span><span class=\"hl-identifier\">log<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-reserved\">new<\/span><span class=\"hl-code\"> <\/span><span class=\"hl-builtin\">Date<\/span><span class=\"hl-brackets\">(<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-brackets\">)<\/span><span class=\"hl-code\">;<\/span><\/pre>\n<\/div>\n<p>Result in Firebug's console:<\/p>\n<p><img decoding=\"async\" id=\"image343\" src=\"https:\/\/www.phpied.com\/wp-content\/uploads\/2008\/03\/sleep.png\" alt=\"sleep.png\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP has a sleep() function, but JavaScript doesn&#8217;t. Well, this is because it&#8217;s useless, you might say, and you&#8217;ll be right. But for simulating heavy processing and for misc performance measurements, it could be useful. So here&#8217;s how you can go about creating a sleep() in JavaScript. The code function sleep(milliseconds) { var start = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,43],"tags":[],"_links":{"self":[{"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/posts\/344"}],"collection":[{"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/comments?post=344"}],"version-history":[{"count":0,"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/posts\/344\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/media?parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/categories?post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpied.com\/wp-json\/wp\/v2\/tags?post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}