{"id":138,"date":"2011-07-01T16:29:54","date_gmt":"2011-07-01T16:29:54","guid":{"rendered":"http:\/\/patternweaver.com\/blog\/?p=138"},"modified":"2011-12-19T07:32:21","modified_gmt":"2011-12-19T07:32:21","slug":"function-whentrue","status":"publish","type":"post","link":"http:\/\/patternweaver.com\/blog\/2011\/07\/function-whentrue\/","title":{"rendered":"Function.whenTrue"},"content":{"rendered":"<p>Sometimes you&#8217;re in JS and you want to perform some task once something else returns (as is reasonable in an asynchronous world), but if you just fire off a bunch of intervals, you&#8217;ll eventually choke the browser. My solution is to add geometric timer falloff and a maximum timeout. this way the longer the task takes the less work we do as we wait, and at some point (in this example ~16 seconds) it gives up. This strategy has been working well for me.<\/p>\n<div id=\"gist1495861\" class=\"gist\">\n    <div class=\"gist-file\">\n      <div class=\"gist-data\">\n        <div class=\"js-gist-file-update-container js-task-list-container file-box\">\n  <div id=\"file-function-whentrue-js\" class=\"file\">\n    \n\n  <div class=\"blob-wrapper data type-javascript\">\n      <table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n      <tr>\n        <td id=\"file-function-whentrue-js-L1\" class=\"blob-num js-line-number\" data-line-number=\"1\"><\/td>\n        <td id=\"file-function-whentrue-js-LC1\" class=\"blob-code blob-code-inner js-file-line\"><span class=\"pl-k\">if<\/span>(<span class=\"pl-k\">!<\/span><span class=\"pl-c1\">Function<\/span>.<span class=\"pl-c1\">actionTimeout<\/span>) <span class=\"pl-c1\">Function<\/span>.<span class=\"pl-c1\">actionTimeout<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-c1\">16384<\/span>;<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L2\" class=\"blob-num js-line-number\" data-line-number=\"2\"><\/td>\n        <td id=\"file-function-whentrue-js-LC2\" class=\"blob-code blob-code-inner js-file-line\"><span class=\"pl-k\">if<\/span>(<span class=\"pl-k\">!<\/span><span class=\"pl-c1\">Function<\/span>.<span class=\"pl-c1\">whenTrue<\/span>){<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L3\" class=\"blob-num js-line-number\" data-line-number=\"3\"><\/td>\n        <td id=\"file-function-whentrue-js-LC3\" class=\"blob-code blob-code-inner js-file-line\">    <span class=\"pl-c1\">Function<\/span>.<span class=\"pl-c1\">implement<\/span>({<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L4\" class=\"blob-num js-line-number\" data-line-number=\"4\"><\/td>\n        <td id=\"file-function-whentrue-js-LC4\" class=\"blob-code blob-code-inner js-file-line\">        <span class=\"pl-en\">whenTrue<\/span> <span class=\"pl-k\">:<\/span> <span class=\"pl-k\">function<\/span>(<span class=\"pl-smi\">actionFunction<\/span>, <span class=\"pl-smi\">args<\/span>, <span class=\"pl-smi\">delayFunction<\/span>, <span class=\"pl-smi\">timeoutFunction<\/span>, <span class=\"pl-smi\">timeout<\/span>, <span class=\"pl-smi\">counter<\/span>){<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L5\" class=\"blob-num js-line-number\" data-line-number=\"5\"><\/td>\n        <td id=\"file-function-whentrue-js-LC5\" class=\"blob-code blob-code-inner js-file-line\">            <span class=\"pl-k\">if<\/span>(<span class=\"pl-k\">!<\/span>timeout) timeout <span class=\"pl-k\">=<\/span> <span class=\"pl-c1\">Function<\/span>.<span class=\"pl-c1\">actionTimeout<\/span>;<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L6\" class=\"blob-num js-line-number\" data-line-number=\"6\"><\/td>\n        <td id=\"file-function-whentrue-js-LC6\" class=\"blob-code blob-code-inner js-file-line\">            <span class=\"pl-k\">if<\/span>(<span class=\"pl-k\">!<\/span>counter) counter <span class=\"pl-k\">=<\/span> <span class=\"pl-c1\">0<\/span>;<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L7\" class=\"blob-num js-line-number\" data-line-number=\"7\"><\/td>\n        <td id=\"file-function-whentrue-js-LC7\" class=\"blob-code blob-code-inner js-file-line\">            <span class=\"pl-k\">if<\/span>(<span class=\"pl-k\">!<\/span>timeoutFunction) <span class=\"pl-en\">timeoutFunction<\/span> <span class=\"pl-k\">=<\/span> <span class=\"pl-k\">function<\/span>(<span class=\"pl-smi\">event<\/span>){<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L8\" class=\"blob-num js-line-number\" data-line-number=\"8\"><\/td>\n        <td id=\"file-function-whentrue-js-LC8\" class=\"blob-code blob-code-inner js-file-line\">                <span class=\"pl-k\">throw<\/span>(<span class=\"pl-s\"><span class=\"pl-pds\">&#39;<\/span>Condition not met after <span class=\"pl-pds\">&#39;<\/span><\/span><span class=\"pl-k\">+<\/span><span class=\"pl-c1\">event<\/span><span class=\"pl-c1\">.time<\/span><span class=\"pl-k\">+<\/span><span class=\"pl-s\"><span class=\"pl-pds\">&#39;<\/span>ms<span class=\"pl-pds\">&#39;<\/span><\/span>);<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L9\" class=\"blob-num js-line-number\" data-line-number=\"9\"><\/td>\n        <td id=\"file-function-whentrue-js-LC9\" class=\"blob-code blob-code-inner js-file-line\">            };<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L10\" class=\"blob-num js-line-number\" data-line-number=\"10\"><\/td>\n        <td id=\"file-function-whentrue-js-LC10\" class=\"blob-code blob-code-inner js-file-line\">            <span class=\"pl-k\">var<\/span> result <span class=\"pl-k\">=<\/span> <span class=\"pl-v\">this<\/span>();<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L11\" class=\"blob-num js-line-number\" data-line-number=\"11\"><\/td>\n        <td id=\"file-function-whentrue-js-LC11\" class=\"blob-code blob-code-inner js-file-line\">            <span class=\"pl-k\">if<\/span>(<span class=\"pl-k\">!<\/span>result){<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L12\" class=\"blob-num js-line-number\" data-line-number=\"12\"><\/td>\n        <td id=\"file-function-whentrue-js-LC12\" class=\"blob-code blob-code-inner js-file-line\">                <span class=\"pl-k\">var<\/span> delayTime <span class=\"pl-k\">=<\/span> <span class=\"pl-c1\">Math<\/span>.<span class=\"pl-c1\">pow<\/span>(<span class=\"pl-c1\">2<\/span>, counter); <span class=\"pl-c\">\/\/ geometric falloff<\/span><\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L13\" class=\"blob-num js-line-number\" data-line-number=\"13\"><\/td>\n        <td id=\"file-function-whentrue-js-LC13\" class=\"blob-code blob-code-inner js-file-line\">                <span class=\"pl-k\">if<\/span>(delayTime <span class=\"pl-k\">&gt;=<\/span> timeout){<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L14\" class=\"blob-num js-line-number\" data-line-number=\"14\"><\/td>\n        <td id=\"file-function-whentrue-js-LC14\" class=\"blob-code blob-code-inner js-file-line\">                    <span class=\"pl-c1\">timeoutFunction<\/span>({<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L15\" class=\"blob-num js-line-number\" data-line-number=\"15\"><\/td>\n        <td id=\"file-function-whentrue-js-LC15\" class=\"blob-code blob-code-inner js-file-line\">                        count <span class=\"pl-k\">:<\/span> counter,<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L16\" class=\"blob-num js-line-number\" data-line-number=\"16\"><\/td>\n        <td id=\"file-function-whentrue-js-LC16\" class=\"blob-code blob-code-inner js-file-line\">                        time <span class=\"pl-k\">:<\/span> delayTime<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L17\" class=\"blob-num js-line-number\" data-line-number=\"17\"><\/td>\n        <td id=\"file-function-whentrue-js-LC17\" class=\"blob-code blob-code-inner js-file-line\">                    });<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L18\" class=\"blob-num js-line-number\" data-line-number=\"18\"><\/td>\n        <td id=\"file-function-whentrue-js-LC18\" class=\"blob-code blob-code-inner js-file-line\">                    <span class=\"pl-k\">return<\/span>;<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L19\" class=\"blob-num js-line-number\" data-line-number=\"19\"><\/td>\n        <td id=\"file-function-whentrue-js-LC19\" class=\"blob-code blob-code-inner js-file-line\">                }<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L20\" class=\"blob-num js-line-number\" data-line-number=\"20\"><\/td>\n        <td id=\"file-function-whentrue-js-LC20\" class=\"blob-code blob-code-inner js-file-line\">                counter<span class=\"pl-k\">++<\/span>;<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L21\" class=\"blob-num js-line-number\" data-line-number=\"21\"><\/td>\n        <td id=\"file-function-whentrue-js-LC21\" class=\"blob-code blob-code-inner js-file-line\">                <span class=\"pl-v\">this<\/span>.<span class=\"pl-c1\">whenTrue<\/span>.<span class=\"pl-c1\">delay<\/span>(delayTime, <span class=\"pl-v\">this<\/span>, [actionFunction, args, delayFunction, timeoutFunction, timeout, counter]);<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L22\" class=\"blob-num js-line-number\" data-line-number=\"22\"><\/td>\n        <td id=\"file-function-whentrue-js-LC22\" class=\"blob-code blob-code-inner js-file-line\">                <span class=\"pl-k\">if<\/span>(delayFunction) <span class=\"pl-c1\">delayFunction<\/span>({<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L23\" class=\"blob-num js-line-number\" data-line-number=\"23\"><\/td>\n        <td id=\"file-function-whentrue-js-LC23\" class=\"blob-code blob-code-inner js-file-line\">                    count <span class=\"pl-k\">:<\/span> counter,<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L24\" class=\"blob-num js-line-number\" data-line-number=\"24\"><\/td>\n        <td id=\"file-function-whentrue-js-LC24\" class=\"blob-code blob-code-inner js-file-line\">                    time <span class=\"pl-k\">:<\/span> delayTime<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L25\" class=\"blob-num js-line-number\" data-line-number=\"25\"><\/td>\n        <td id=\"file-function-whentrue-js-LC25\" class=\"blob-code blob-code-inner js-file-line\">                });<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L26\" class=\"blob-num js-line-number\" data-line-number=\"26\"><\/td>\n        <td id=\"file-function-whentrue-js-LC26\" class=\"blob-code blob-code-inner js-file-line\">            }<span class=\"pl-k\">else<\/span>{<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L27\" class=\"blob-num js-line-number\" data-line-number=\"27\"><\/td>\n        <td id=\"file-function-whentrue-js-LC27\" class=\"blob-code blob-code-inner js-file-line\">                actionFunction.<span class=\"pl-c1\">apply<\/span>(<span class=\"pl-v\">this<\/span>, args);<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L28\" class=\"blob-num js-line-number\" data-line-number=\"28\"><\/td>\n        <td id=\"file-function-whentrue-js-LC28\" class=\"blob-code blob-code-inner js-file-line\">            }<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L29\" class=\"blob-num js-line-number\" data-line-number=\"29\"><\/td>\n        <td id=\"file-function-whentrue-js-LC29\" class=\"blob-code blob-code-inner js-file-line\">        }<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L30\" class=\"blob-num js-line-number\" data-line-number=\"30\"><\/td>\n        <td id=\"file-function-whentrue-js-LC30\" class=\"blob-code blob-code-inner js-file-line\">    });<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue-js-L31\" class=\"blob-num js-line-number\" data-line-number=\"31\"><\/td>\n        <td id=\"file-function-whentrue-js-LC31\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n      <\/tr>\n<\/table>\n\n  <\/div>\n\n  <\/div>\n  \n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/khrome\/1495861\/raw\/14df9ef0efaeddd354f1241180ae8a31aa14944e\/Function.whenTrue.js\" style=\"float:right\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/khrome\/1495861#file-function-whentrue-js\">Function.whenTrue.js<\/a>\n        hosted with &#10084; by <a href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n\n<p>Then call it like:<\/p>\n<p><div id=\"gist1495861\" class=\"gist\">\n    <div class=\"gist-file\">\n      <div class=\"gist-data\">\n        <div class=\"js-gist-file-update-container js-task-list-container file-box\">\n  <div id=\"file-function-whentrue_ex-js\" class=\"file\">\n    \n\n  <div class=\"blob-wrapper data type-javascript\">\n      <table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n      <tr>\n        <td id=\"file-function-whentrue_ex-js-L1\" class=\"blob-num js-line-number\" data-line-number=\"1\"><\/td>\n        <td id=\"file-function-whentrue_ex-js-LC1\" class=\"blob-code blob-code-inner js-file-line\">(<span class=\"pl-k\">function<\/span>(){<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue_ex-js-L2\" class=\"blob-num js-line-number\" data-line-number=\"2\"><\/td>\n        <td id=\"file-function-whentrue_ex-js-LC2\" class=\"blob-code blob-code-inner js-file-line\">    <span class=\"pl-c\">\/\/code here<\/span><\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue_ex-js-L3\" class=\"blob-num js-line-number\" data-line-number=\"3\"><\/td>\n        <td id=\"file-function-whentrue_ex-js-LC3\" class=\"blob-code blob-code-inner js-file-line\">}).<span class=\"pl-c1\">whenTrue<\/span>(<span class=\"pl-k\">function<\/span>(<span class=\"pl-smi\">arg1<\/span>){<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue_ex-js-L4\" class=\"blob-num js-line-number\" data-line-number=\"4\"><\/td>\n        <td id=\"file-function-whentrue_ex-js-LC4\" class=\"blob-code blob-code-inner js-file-line\">    <span class=\"pl-c\">\/\/code here<\/span><\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue_ex-js-L5\" class=\"blob-num js-line-number\" data-line-number=\"5\"><\/td>\n        <td id=\"file-function-whentrue_ex-js-LC5\" class=\"blob-code blob-code-inner js-file-line\">}, [val1]);<\/td>\n      <\/tr>\n<\/table>\n\n  <\/div>\n\n  <\/div>\n  \n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/khrome\/1495861\/raw\/14df9ef0efaeddd354f1241180ae8a31aa14944e\/Function.whenTrue_ex.js\" style=\"float:right\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/khrome\/1495861#file-function-whentrue_ex-js\">Function.whenTrue_ex.js<\/a>\n        hosted with &#10084; by <a href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<br \/>\nor<br \/>\n<div id=\"gist1495861\" class=\"gist\">\n    <div class=\"gist-file\">\n      <div class=\"gist-data\">\n        <div class=\"js-gist-file-update-container js-task-list-container file-box\">\n  <div id=\"file-function-whentrue_ex2-js\" class=\"file\">\n    \n\n  <div class=\"blob-wrapper data type-javascript\">\n      <table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n      <tr>\n        <td id=\"file-function-whentrue_ex2-js-L1\" class=\"blob-num js-line-number\" data-line-number=\"1\"><\/td>\n        <td id=\"file-function-whentrue_ex2-js-LC1\" class=\"blob-code blob-code-inner js-file-line\">myFunction.<span class=\"pl-c1\">whenTrue<\/span>(<span class=\"pl-k\">function<\/span>(<span class=\"pl-smi\">arg1<\/span>){<\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue_ex2-js-L2\" class=\"blob-num js-line-number\" data-line-number=\"2\"><\/td>\n        <td id=\"file-function-whentrue_ex2-js-LC2\" class=\"blob-code blob-code-inner js-file-line\">     <span class=\"pl-c\">\/\/code here<\/span><\/td>\n      <\/tr>\n      <tr>\n        <td id=\"file-function-whentrue_ex2-js-L3\" class=\"blob-num js-line-number\" data-line-number=\"3\"><\/td>\n        <td id=\"file-function-whentrue_ex2-js-LC3\" class=\"blob-code blob-code-inner js-file-line\">}, [val1]);<\/td>\n      <\/tr>\n<\/table>\n\n  <\/div>\n\n  <\/div>\n  \n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/khrome\/1495861\/raw\/14df9ef0efaeddd354f1241180ae8a31aa14944e\/Function.whenTrue_ex2.js\" style=\"float:right\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/khrome\/1495861#file-function-whentrue_ex2-js\">Function.whenTrue_ex2.js<\/a>\n        hosted with &#10084; by <a href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n<p>Enjoy,<br \/>\n    -abbey<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you&#8217;re in JS and you want to perform some task once something else returns (as is reasonable in an asynchronous world), but if you just fire off a bunch of intervals, you&#8217;ll eventually choke the browser. My solution is to add geometric timer falloff and a maximum timeout. this way the longer the task [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,11],"tags":[],"class_list":["post-138","post","type-post","status-publish","format-standard","hentry","category-js","category-mootools"],"_links":{"self":[{"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/posts\/138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/comments?post=138"}],"version-history":[{"count":0,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"wp:attachment":[{"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/patternweaver.com\/blog\/wp-json\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}