index.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?PHP
  2. include "../index.php";
  3. $shell['title3'] = "Throttle";
  4. $shell['h2'] = 'Don\'t fire that event any more than you have to!';
  5. // ========================================================================== //
  6. // SCRIPT
  7. // ========================================================================== //
  8. ob_start();
  9. ?>
  10. $(function(){
  11. var counter_1 = 0,
  12. counter_2 = 0
  13. last_time_1 = +new Date(),
  14. last_time_2 = +new Date();
  15. // This function is not throttled, but instead bound directly to the event.
  16. function resize_1() {
  17. var now = +new Date(),
  18. html = 'resize handler executed: ' + counter_1++ + ' times'
  19. + ' (' + ( now - last_time_1 ) + 'ms since previous execution)'
  20. + '<br/>window dimensions: ' + $(window).width() + 'x' + $(window).height();
  21. last_time_1 = now;
  22. $('#text-resize-1').html( html );
  23. };
  24. // This function is throttled, and the new, throttled, function is bound to
  25. // the event. Note that in jQuery 1.4+ a reference to either the original or
  26. // throttled function can be passed to .unbind to unbind the function.
  27. function resize_2() {
  28. var now = +new Date(),
  29. html = 'throttled resize handler executed: ' + counter_2++ + ' times'
  30. + ' (' + ( now - last_time_2 ) + 'ms since previous execution)'
  31. + '<br/>window dimensions: ' + $(window).width() + 'x' + $(window).height();
  32. last_time_2 = now;
  33. $('#text-resize-2').html( html );
  34. };
  35. // Bind the not-at-all throttled handler to the resize event.
  36. $(window).resize( resize_1 );
  37. // Bind the throttled handler to the resize event.
  38. $(window).resize( $.throttle( 250, resize_2 ) ); // This is the line you want!
  39. });
  40. <?
  41. $shell['script1'] = ob_get_contents();
  42. ob_end_clean();
  43. ob_start();
  44. ?>
  45. $(function(){
  46. var counter_1 = 0,
  47. counter_2 = 0
  48. last_time_1 = +new Date(),
  49. last_time_2 = +new Date();
  50. // This function is not throttled, but instead bound directly to the event.
  51. function scroll_1() {
  52. var now = +new Date(),
  53. html = 'scroll handler executed: ' + counter_1++ + ' times'
  54. + ' (' + ( now - last_time_1 ) + 'ms since previous execution)'
  55. + '<br/>window scrollLeft: ' + $(window).scrollLeft() + ', scrollTop: ' + $(window).scrollTop();
  56. last_time_1 = now;
  57. $('#text-scroll-1').html( html );
  58. };
  59. // This function is throttled, and the new, throttled, function is bound to
  60. // the event. Note that in jQuery 1.4+ a reference to either the original or
  61. // throttled function can be passed to .unbind to unbind the function.
  62. function scroll_2() {
  63. var now = +new Date(),
  64. html = 'throttled scroll handler executed: ' + counter_2++ + ' times'
  65. + ' (' + ( now - last_time_2 ) + 'ms since previous execution)'
  66. + '<br/>window scrollLeft: ' + $(window).scrollLeft() + ', scrollTop: ' + $(window).scrollTop();
  67. last_time_2 = now;
  68. $('#text-scroll-2').html( html );
  69. };
  70. // Bind the not-at-all throttled handler to the scroll event.
  71. $(window).scroll( scroll_1 );
  72. // Bind the throttled handler to the scroll event.
  73. $(window).scroll( $.throttle( 250, scroll_2 ) ); // This is the line you want!
  74. });
  75. <?
  76. $shell['script2'] = ob_get_contents();
  77. ob_end_clean();
  78. // ========================================================================== //
  79. // HTML HEAD ADDITIONAL
  80. // ========================================================================== //
  81. ob_start();
  82. ?>
  83. <script type="text/javascript" src="../../jquery.ba-throttle-debounce.js"></script>
  84. <script type="text/javascript" language="javascript">
  85. <?= $shell['script1']; ?>
  86. <?= $shell['script2']; ?>
  87. $(function(){
  88. // Trigger these events once to show some initial (zero) values.
  89. $(window).resize().scroll();
  90. // Syntax highlighter.
  91. SyntaxHighlighter.highlight();
  92. });
  93. </script>
  94. <style type="text/css" title="text/css">
  95. /*
  96. bg: #FDEBDC
  97. bg1: #FFD6AF
  98. bg2: #FFAB59
  99. orange: #FF7F00
  100. brown: #913D00
  101. lt. brown: #C4884F
  102. */
  103. #page {
  104. width: 700px;
  105. }
  106. </style>
  107. <?
  108. $shell['html_head'] = ob_get_contents();
  109. ob_end_clean();
  110. // ========================================================================== //
  111. // HTML BODY
  112. // ========================================================================== //
  113. ob_start();
  114. ?>
  115. <?= $shell['donate'] ?>
  116. <p>
  117. Using <a href="http://benalman.com/projects/jquery-throttle-debounce-plugin/">jQuery throttle / debounce</a>, you can pass a delay and function to <code>$.throttle</code> to get a new function, that when called repetitively, executes the original function (in the same context and with all arguments passed through) no more than once every delay milliseconds.
  118. </p>
  119. <p>
  120. This can be especially useful for rate limiting execution of handlers on events like resize and scroll. Just take a look at the examples to see for yourself!
  121. </p>
  122. <h3>Window resize (some browsers fire this event continually)</h3>
  123. <p id="text-resize-1"></p>
  124. <p id="text-resize-2"></p>
  125. <h3>The code</h3>
  126. <div class="clear"></div>
  127. <pre class="brush:js">
  128. <?= htmlspecialchars( $shell['script1'] ); ?>
  129. </pre>
  130. <h3>Window scroll (some browsers fire this event continually)</h3>
  131. <p id="text-scroll-1"></p>
  132. <p id="text-scroll-2"></p>
  133. <h3>The code</h3>
  134. <pre class="brush:js">
  135. <?= htmlspecialchars( $shell['script2'] ); ?>
  136. </pre>
  137. <?
  138. $shell['html_body'] = ob_get_contents();
  139. ob_end_clean();
  140. // ========================================================================== //
  141. // DRAW SHELL
  142. // ========================================================================== //
  143. draw_shell();
  144. ?>