| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <title>dojox.timing.doLater</title> |
| 5 | <style type="text/css"> |
| 6 | @import "../../../dojo/resources/dojo.css"; |
| 7 | @import "../../../dijit/tests/css/dijitTests.css"; |
| 8 | </style> |
| 9 | <script type="text/javascript" src="../../../dojo/dojo.js" data-dojo-config="isDebug:true" ></script> |
| 10 | <script> |
| 11 | dojo.require("dojox.timing.doLater"); |
| 12 | |
| 13 | |
| 14 | /*======================== |
| 15 | TEST 1 - Global space |
| 16 | =========================*/ |
| 17 | |
| 18 | // triggers our pseudo "isReady" |
| 19 | setTimeout(function(){ pseudoReadyThing = true; },700); |
| 20 | |
| 21 | var objectId ="global"; // objectId is used to check we are in the correct context. |
| 22 | var pseudoReadyThing = false; // our global "ready" conditional |
| 23 | |
| 24 | // testing function in global context |
| 25 | var doThing = function(first, last){ |
| 26 | if(dojox.timing.doLater(pseudoReadyThing)){return;} // our key statement |
| 27 | console.log("doThing:", objectId, "::", first, last); // success! |
| 28 | } |
| 29 | |
| 30 | // invoke! |
| 31 | doThing("Selina", "Kyle"); // invoke doThing, although it will not be ready for 700ms |
| 32 | |
| 33 | /*===================== |
| 34 | TEST 2 - context |
| 35 | ======================*/ |
| 36 | |
| 37 | // triggers our "onReady" |
| 38 | setTimeout(function(){ app.ready = true; },900); |
| 39 | |
| 40 | // testing a function within an object context |
| 41 | var app = { |
| 42 | objectId:"app", |
| 43 | ready:false, |
| 44 | doStuff: function(first, last){ |
| 45 | if(dojox.timing.doLater(this.ready, this)){return;} |
| 46 | console.log("app.doStuff:", this.objectId, "::", first, last) |
| 47 | }, |
| 48 | goCrazy: function(first, last){ |
| 49 | if(dojox.timing.doLater(this.ready, this)){return;} |
| 50 | console.log("app.goCrazy:", this.objectId, "::", first, last); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // These three calls happen (almost) simultaneously |
| 55 | // to test that they don't clobber each other |
| 56 | app.doStuff("Oswald", "Cobblepot"); |
| 57 | app.doStuff("Edward", "Nigma"); |
| 58 | app.goCrazy("Mike", "Wilcox"); |
| 59 | |
| 60 | /*===================== |
| 61 | Misc Tests |
| 62 | ======================*/ |
| 63 | |
| 64 | // NOTE: the timeouts used to invoke these tests cause the |
| 65 | // results to sometimes return in an unpredictable order. |
| 66 | |
| 67 | // testing an anonymous function |
| 68 | setTimeout(function(){ |
| 69 | if(dojox.timing.doLater(app.ready)){return;} |
| 70 | console.log("anonymous.function SUCCESS") |
| 71 | },700); |
| 72 | |
| 73 | // testing conditional usage OR |
| 74 | setTimeout(function(){ |
| 75 | if(dojox.timing.doLater(pseudoReadyThing || app.ready)){return;} |
| 76 | console.log("pseudoReadyThing OR app.ready is ready") |
| 77 | },0); |
| 78 | |
| 79 | // testing conditional usage AND |
| 80 | setTimeout(function(){ |
| 81 | if(dojox.timing.doLater(pseudoReadyThing && app.ready)){return;} |
| 82 | console.log("pseudoReadyThing AND app.ready is ready") |
| 83 | },0); |
| 84 | |
| 85 | // testing conditional expression, incremented |
| 86 | // essentially, anonymous function will get |
| 87 | // called ten times (every default 100 ms) |
| 88 | var count = 0 |
| 89 | setTimeout(function(){ |
| 90 | if(dojox.timing.doLater(++count>=10)){return;} |
| 91 | console.log("incremented count:", count); |
| 92 | },0); |
| 93 | |
| 94 | // testing random conditional |
| 95 | // anonymous function will be called until |
| 96 | // random result >= 8 |
| 97 | setTimeout(function(){ |
| 98 | if(dojox.timing.doLater(Math.random(1)*10>8)){return;} |
| 99 | console.log("random returned greater than 8"); |
| 100 | },0); |
| 101 | |
| 102 | |
| 103 | </script> |
| 104 | <style> |
| 105 | p{ width:500px; } |
| 106 | .code{ |
| 107 | font-family:monospace; |
| 108 | line-height:16px; |
| 109 | margin-left:20px; |
| 110 | } |
| 111 | </style> |
| 112 | </head> |
| 113 | <body> |
| 114 | <h1>dojox.timing.doLater()</h1> |
| 115 | <p> |
| 116 | There are several tests running in the background - open the console to view. |
| 117 | </p> |
| 118 | <p> |
| 119 | <em>dojox.timing.doLater()</em> provides a simple mechanism that checks a conditional before allowing |
| 120 | your function to continue. If the conditional is false, the function is blocked and continually |
| 121 | re-called, with arguments, until the conditional passes. |
| 122 | </p> |
| 123 | <div> |
| 124 | <em>dojox.timing.doLater()</em> is designed to be used with one key statement (in bold):<br/> |
| 125 | <div class="code"> |
| 126 | var doThing = function(first, last){ |
| 127 | <div class="code"><strong>if(dojox.timing.doLater(</strong><em>pseudoReadyThing</em><strong>)){return;} </strong></div> |
| 128 | <div class="code">doOther(first);</div> |
| 129 | <div class="code">doMore(last);</div> |
| 130 | } |
| 131 | </div> |
| 132 | </div> |
| 133 | <p> |
| 134 | Although these tests are using timers and incremnters, the idea behind |
| 135 | <em>dojox.timing.doLater()</em> is to test for things like if a window is loaded, |
| 136 | or if an XHR has returned. |
| 137 | </p> |
| 138 | |
| 139 | </body> |
| 140 | </html> |