{"id":102,"date":"2008-11-23T00:00:42","date_gmt":"2008-11-23T07:00:42","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=227"},"modified":"2008-11-23T00:00:42","modified_gmt":"2008-11-23T07:00:42","slug":"recursion-trick-w-settimeout","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=102","title":{"rendered":"Recursion Trick w\/ setTimeout()"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar counter:int = 0;\r\n\r\ngraphics.lineStyle(0,0x000000);\r\n\r\nrecursive();\r\n\r\nfunction recursive():void{\r\n\t\r\n\tif (counter &lt; 4000){\r\n\t   graphics.moveTo(10,10);\r\n\t   graphics.lineTo(counter, 200);\r\n\t   counter+=4;\r\n          \/\/ don&#039;t call recursive(), use setTimeout instead\r\n\t   setTimeout(recursive,0);\r\n\t}else{\r\n\t\ttrace(counter \/ 4 + &quot; lines were drawn&quot;);\r\n\t}\r\n<\/pre>\n<p>Be careful with this one, if you don&#8217;t know what your doing you may be able to crash flash.<\/p>\n<p>Anyway&#8230; if you use a recursive function in flash you may find yourself getting a stack overflow error. You can avoid this by using setTimeout():<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n \/\/ don&#039;t call recursive(), use setTimeout() instead\r\nsetTimeout(recursive,0);\r\n<\/pre>\n<p>Of course&#8230; the stack overflow error is there for a reason, if used incorrectly you could bring the flash player to a standstill. <\/p>\n<p>You can also achieve animated recursion by actually adding a delay to your setTimout() call:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n \/\/ don&#039;t call recursive(), use setTimeout() instead\r\nsetTimeout(recursive,10);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>var counter:int = 0; graphics.lineStyle(0,0&#215;000000); recursive(); function recursive():void{ if (counter &lt; 4000){ graphics.moveTo(10,10); graphics.lineTo(counter, 200); counter+=4; \/\/ don&#039;t call recursive(), use setTimeout instead setTimeout(recursive,0); }else{ trace(counter \/ 4 + &quot; lines were drawn&quot;); } Be careful with this one, if you don&#8217;t know what your doing you may be able to crash flash. Anyway&#8230; if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[3,4,65,66],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-functions","tag-actionscript","tag-flash","tag-recursion","tag-settimeout"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=102"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/102\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}