PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/examples/delay-poll/index.php

https://github.com/rwaldron/jquery-dotimeout
PHP | 348 lines | 265 code | 48 blank | 35 comment | 1 complexity | 5e53c5bbe14fec764eeafc39173ee4d4 MD5 | raw file
Possible License(s): GPL-2.0, MIT
  1. <?PHP
  2. include "../index.php";
  3. $shell['title3'] = "Delays &amp; Polling Loops";
  4. $shell['h2'] = 'For when setTimeout just isn\'t good enough!';
  5. // ========================================================================== //
  6. // SCRIPT
  7. // ========================================================================== //
  8. ob_start();
  9. ?>
  10. $('#set_timeout1 a.start').click(function(){
  11. // In one second, do something!
  12. $.doTimeout( 1000, function(){
  13. $('#set_timeout1 span').html( 'done!' );
  14. });
  15. });
  16. <?
  17. $shell['script1'] = ob_get_contents();
  18. ob_end_clean();
  19. ob_start();
  20. ?>
  21. $('#set_timeout2 a.start').click(function(){
  22. // Execute .css( 'color', 'blue' ), now then in
  23. // one second, execute .css( 'color', 'blue' ).
  24. $('#set_timeout2 span')
  25. .css( 'color', 'red' )
  26. .doTimeout( 1000, function(){
  27. this.css( 'color', 'blue' )
  28. });
  29. });
  30. <?
  31. $shell['script2'] = ob_get_contents();
  32. ob_end_clean();
  33. ob_start();
  34. ?>
  35. $('#set_timeout2a a.start').click(function(){
  36. // Execute .css( 'color', 'blue' ), now then in
  37. // one second, execute .css( 'color', 'blue' ).
  38. $('#set_timeout2a span')
  39. .css( 'color', 'red' )
  40. .doTimeout( 1000, 'css', 'color', 'blue' );
  41. });
  42. <?
  43. $shell['script2a'] = ob_get_contents();
  44. ob_end_clean();
  45. ob_start();
  46. ?>
  47. var counter1 = 0;
  48. $('#polling_loop1 a.start').click(function(){
  49. // Start a polling loop with a counter.
  50. $.doTimeout( 250, function(){
  51. $('#polling_loop1 span').html( ++counter1 );
  52. return true; // Poll.
  53. });
  54. });
  55. <?
  56. $shell['script3'] = ob_get_contents();
  57. ob_end_clean();
  58. ob_start();
  59. ?>
  60. var counter2 = 0;
  61. $('#polling_loop2 a.start').click(function(){
  62. // Start a polling loop with a counter.
  63. $.doTimeout( 250, function(){
  64. $('#polling_loop2 span').html( ++counter2 );
  65. if ( counter2 < 50 ) { return true; } // Poll.
  66. });
  67. });
  68. <?
  69. $shell['script4'] = ob_get_contents();
  70. ob_end_clean();
  71. ob_start();
  72. ?>
  73. $('#polling_loop3 a.start').click(function(){
  74. // Start a polling loop with an id of 'loop' and a counter.
  75. var i = 0;
  76. $.doTimeout( 'loop', 500, function(){
  77. $('#polling_loop3 span').html( ++i );
  78. return true;
  79. });
  80. });
  81. $('#polling_loop3 a.force').click(function(){
  82. // Prematurely force execution of next polling loop iteration.
  83. $.doTimeout( 'loop', true );
  84. });
  85. $('#polling_loop3 a.cancel').click(function(){
  86. // Cancel the polling loop with id of 'loop'.
  87. $.doTimeout( 'loop' );
  88. });
  89. <?
  90. $shell['script5'] = ob_get_contents();
  91. ob_end_clean();
  92. ob_start();
  93. ?>
  94. var elem = $('#polling_loop4');
  95. elem.find('a.start').click(function(){
  96. // Start a polling loop with an id of 'loop' and a counter.
  97. var i = 0;
  98. elem.doTimeout( 'loop', 500, function(){
  99. this.find('span').html( ++i );
  100. return true;
  101. });
  102. });
  103. elem.find('a.force').click(function(){
  104. // Prematurely force execution of next polling loop iteration.
  105. elem.doTimeout( 'loop', true );
  106. });
  107. elem.find('a.cancel').click(function(){
  108. // Cancel the polling loop with id of 'loop'.
  109. elem.doTimeout( 'loop' );
  110. });
  111. <?
  112. $shell['script6'] = ob_get_contents();
  113. ob_end_clean();
  114. // ========================================================================== //
  115. // HTML HEAD ADDITIONAL
  116. // ========================================================================== //
  117. ob_start();
  118. ?>
  119. <script type="text/javascript" src="../../jquery.ba-dotimeout.js"></script>
  120. <script type="text/javascript" language="javascript">
  121. $(function(){
  122. <?= $shell['script1']; ?>
  123. <?= $shell['script2']; ?>
  124. <?= $shell['script2a']; ?>
  125. <?= $shell['script3']; ?>
  126. <?= $shell['script4']; ?>
  127. <?= $shell['script5']; ?>
  128. <?= $shell['script6']; ?>
  129. $('a[href=#]').click(function(e){
  130. e.preventDefault();
  131. });
  132. $('a.single').click(function(){
  133. $(this).replaceWith( 'Started!' );
  134. });
  135. // Syntax highlighter.
  136. SyntaxHighlighter.highlight();
  137. });
  138. </script>
  139. <style type="text/css" title="text/css">
  140. /*
  141. bg: #FDEBDC
  142. bg1: #FFD6AF
  143. bg2: #FFAB59
  144. orange: #FF7F00
  145. brown: #913D00
  146. lt. brown: #C4884F
  147. */
  148. #page {
  149. width: 800px;
  150. }
  151. pre, div.syntaxhighlighter {
  152. float: right !important;
  153. width: 450px !important;
  154. margin-left: 1em !important;
  155. }
  156. div.hr {
  157. clear: both;
  158. }
  159. </style>
  160. <?
  161. $shell['html_head'] = ob_get_contents();
  162. ob_end_clean();
  163. // ========================================================================== //
  164. // HTML BODY
  165. // ========================================================================== //
  166. ob_start();
  167. ?>
  168. <?= $shell['donate'] ?>
  169. <p>
  170. Here are some simple delay and polling loop examples, utilizing doTimeout. For more usage examples, also see the <a href="http://benalman.com/projects/jquery-dotimeout-plugin/">doTimeout plugin page</a>.
  171. </p>
  172. <div class="test clear">
  173. <h3>Like setTimeout, but better!</h3>
  174. <pre class="brush:js">
  175. <?= htmlspecialchars( $shell['script1'] ); ?>
  176. </pre>
  177. <div id="set_timeout1">
  178. <p>Set value: <span>???</span></p>
  179. <p><a href="#" class="start single">Start</a></p>
  180. <p>
  181. This works exactly like setTimeout, except that you can pass additional callback arguments
  182. as parameters to doTimeout, which will actually be passed into the callback in all browsers!
  183. (setTimeout doesn't quite get this right in all browsers).
  184. </p>
  185. </div>
  186. <div class="hr"><hr></div>
  187. <h3>Like setTimeout, but on a jQuery object</h3>
  188. <pre class="brush:js">
  189. <?= htmlspecialchars( $shell['script2'] ); ?>
  190. </pre>
  191. <div id="set_timeout2">
  192. <p>Set value: <span>Some text</span>, <span>more text</span></p>
  193. <p><a href="#" class="start">Start</a></p>
  194. <p>
  195. Much like setTimeout or the example above, except that in the callback, `this`
  196. refers to the jQuery object. Chainable, too!
  197. </p>
  198. </div>
  199. <div class="hr"><hr></div>
  200. <h3>An even easier syntax!</h3>
  201. <pre class="brush:js">
  202. <?= htmlspecialchars( $shell['script2a'] ); ?>
  203. </pre>
  204. <div id="set_timeout2a">
  205. <p>Set value: <span>Some text</span>, <span>more text</span></p>
  206. <p><a href="#" class="start">Start</a></p>
  207. <p>
  208. Much like setTimeout or the example above, except with a much more concise
  209. "string method" syntax. Still chainable!
  210. </p>
  211. </div>
  212. <div class="hr"><hr></div>
  213. <h3>Basic polling loop (somewhat like setInterval.. but different)</h3>
  214. <pre class="brush:js">
  215. <?= htmlspecialchars( $shell['script3'] ); ?>
  216. </pre>
  217. <div id="polling_loop1">
  218. <p>Polling loop counter: <span>???</span></p>
  219. <p><a href="#" class="start single">Start</a></p>
  220. <p>
  221. If you need to-the-millisecond loop timing accuracy, use setInterval, but beware that
  222. this may have unwanted side effects. For example, if the callback execution time takes
  223. longer than the delay (due to slow code or late execution), the next callback might
  224. execute immediately afterwards, which is almost never what you want in a polling loop!
  225. </p>
  226. <p>
  227. So.. for practical polling loops, use doTimeout. All you need to do is return true,
  228. and doTimeout does the rest!
  229. </p>
  230. </div>
  231. <div class="hr"><hr></div>
  232. <h3>Basic polling loop, with a built-in limit</h3>
  233. <pre class="brush:js">
  234. <?= htmlspecialchars( $shell['script4'] ); ?>
  235. </pre>
  236. <div id="polling_loop2">
  237. <p>Polling loop counter: <span>???</span></p>
  238. <p><a href="#" class="start single">Start</a></p>
  239. <p>
  240. This polling loop example is like the simple example above, except that the loop
  241. is automatically canceled after 50 iterations. This condition should, of course,
  242. be something actually useful.. but for this example, a simple counter does the trick.
  243. </p>
  244. </div>
  245. <div class="hr"><hr></div>
  246. <h3>Cancelable polling loop</h3>
  247. <pre class="brush:js">
  248. <?= htmlspecialchars( $shell['script5'] ); ?>
  249. </pre>
  250. <div id="polling_loop3">
  251. <p>Polling loop counter: <span>???</span></p>
  252. <p><a href="#" class="start">(Re)start</a>, <a href="#" class="force">Force</a>, <a href="#" class="cancel">Cancel</a></p>
  253. <p>
  254. Unlike all the preceding doTimeout examples, this example uses an id ("loop") which allows
  255. it to be restarted, overridden, or canceled.
  256. </p>
  257. <p>
  258. Notice that once started, restarting the loop will first cancel the already-running loop,
  259. keeping things sane. Forcing the loop will cause the callback to be invoked immediately
  260. (continuing the polling loop), and canceling the loop will stop it immediately.
  261. </p>
  262. </div>
  263. <div class="hr"><hr></div>
  264. <h3>Cancelable polling loop, but on a jQuery object</h3>
  265. <pre class="brush:js">
  266. <?= htmlspecialchars( $shell['script6'] ); ?>
  267. </pre>
  268. <div id="polling_loop4">
  269. <p>Polling loop counter: <span>???</span></p>
  270. <p><a href="#" class="start">(Re)start</a>, <a href="#" class="force">Force</a>, <a href="#" class="cancel">Cancel</a></p>
  271. <p>
  272. Much like the example above, except that in the callback, `this` refers to the
  273. jQuery object.
  274. </p>
  275. <p>
  276. Also, notice that when an id is used in doTimeout on a jQuery object,
  277. that id is specific to that object and will not override a $.doTimeout id of
  278. the same name or a different jQuery object's doTimeout id.
  279. </p>
  280. </div>
  281. <?
  282. $shell['html_body'] = ob_get_contents();
  283. ob_end_clean();
  284. // ========================================================================== //
  285. // DRAW SHELL
  286. // ========================================================================== //
  287. draw_shell();
  288. ?>