{"id":25,"date":"2008-03-21T23:06:39","date_gmt":"2008-03-22T04:06:39","guid":{"rendered":"http:\/\/littlesvr.ca\/grumble\/2008\/03\/21\/to-sleep-with-javascript\/"},"modified":"2012-12-05T00:57:41","modified_gmt":"2012-12-05T05:57:41","slug":"to-sleep-with-javascript","status":"publish","type":"post","link":"http:\/\/littlesvr.ca\/grumble\/2008\/03\/21\/to-sleep-with-javascript\/","title":{"rendered":"To sleep with javascript"},"content":{"rendered":"<p>All proper programming languages I can think of have a real simple function &#8211; sleep(). It suspends the execution of your program for a time. This is what I wanted to do. Having no choice but to use javascript in this case, I went around looking for what the sleep() function is called in this dreadful language.<\/p>\n<p>Turns out there is none. There is only setTimeout() and setInterval(). Either will call a function after a time, but in between your code will continue to execute.<\/p>\n<p>All I could find on the internet is a bunch of solutions to this problem involving an infinite loop. That, for I hope obvious reasons, was not acceptable for me.<\/p>\n<p>I considered using `yield` but that doesn&#8217;t work for some reason in my version of the current firefox trunk. I did the javascript version thing, but then none of the javascript I had (yield or not) would work at all.<\/p>\n<p>So I grumbled for a couple of hours and finally figured out a solution so my stuff still works as I want it, and all that&#8217;s lost is readability. I&#8217;ll share this beast with you all in case you ever run into the same problem.<\/p>\n<p>This is what I wanted done in the first place, overly simplfied (lack of whitespace courtesy of wordpress):<\/p>\n<p>function wish(i, j)<br \/>\n{<br \/>\nvar k = i + j;<\/p>\n<p>for(var m = 0; m &lt; 10; m++)<br \/>\n{<br \/>\n\/\/ start doing something outside of my control<\/p>\n<p>sleep(5000); \/\/ sleep for 5 seconds<\/p>\n<p>\/\/ check the results of the something<\/p>\n<p>alert(k + &#8220;, &#8221; + m + results);<br \/>\n}<br \/>\n}<\/p>\n<p>Like I said, it&#8217;s overly simplified. There&#8217;s no need to bring 3D and benchmarks into this example.<\/p>\n<p>Since setInterval() is all I have to work with, that&#8217;s what I&#8217;ll use. The tricks to address my concerns are:<\/p>\n<ol>\n<li>Make a global function that will take the (i, j) parameters and have it call another function (real()) that doesn&#8217;t take any parameters<\/li>\n<li>Copy the parameters to wish() into glabal variables<\/li>\n<li>Where the sleep() would be have a setInterval(&#8216;real()&#8217;, 5000) instead<\/li>\n<li>Copy all the local variables in real() to global variables before the setInterval()<\/li>\n<li>Add a condition so real() will know whether it should continue from where it left off or start from the beginning.<\/li>\n<\/ol>\n<p>A lot of work, but the other option is to not do it at all.. Here&#8217;s what it now looks like:<\/p>\n<p>var gI;<br \/>\nvar gJ;<\/p>\n<p>function wish(i, j)<br \/>\n{<br \/>\ngI = i;<br \/>\ngJ = j;<br \/>\nreal();<br \/>\n}<\/p>\n<p>var gK;<br \/>\nvar gM = 0;<br \/>\nvar didSleep = false;<\/p>\n<p>function real()<br \/>\n{<br \/>\nif (didSleep)<br \/>\n{<br \/>\n\/\/ check the results of the something<br \/>\nalert(gK + &#8220;, &#8221; + (gM &#8211; 1) + results);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\ngK = gI + gJ;<br \/>\n}<\/p>\n<p>for(m = gM; m &lt; 10; m++)<br \/>\n{<br \/>\n\/\/ start doing something outside of my control<\/p>\n<p>gK = k;<br \/>\ngM = m + 1;<br \/>\ndidSleep = true;<br \/>\nsetInterval(&#8220;real()&#8221;, 5000);<br \/>\nreturn;<br \/>\n}<br \/>\n}<\/p>\n<p>That&#8217;s pretty disgusting. I&#8217;d blame the people who came up with javascript if it wasn&#8217;t for the fact that javascript was never designed to be a proper language. Javascript was made for people one level above HTML, which is far, far from real programming and it served those people well.<\/p>\n<p>Instead I blame the people responsible for the javascript hype. Both the fanboys who don&#8217;t know any better but their strength is in numbers; and the programmers who sell their time\/ideas by proclaiming that AJAX is the only way to go. I even blame google a little for making the two useful javascript applications in the world.<\/p>\n<p>I&#8217;ll say it again &#8211; disgusting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All proper programming languages I can think of have a real simple function &#8211; sleep(). It suspends the execution of your program for a time. This is what I wanted to do. Having no choice but to use javascript in this case, I went around looking for what the sleep() function is called in this &hellip; <\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-andrew","4":"post-25","6":"format-standard","7":"category-safeforseneca"},"_links":{"self":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":2,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":670,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/posts\/25\/revisions\/670"}],"wp:attachment":[{"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/littlesvr.ca\/grumble\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}