index.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?PHP
  2. include "../index.php";
  3. $shell['title3'] = "Debounce";
  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 default_text = $('#text-type').text(),
  12. text_counter_1 = 0,
  13. text_counter_2 = 0;
  14. // This function is not debounced, but instead bound directly to the event.
  15. function text_1() {
  16. var val = $(this).val(),
  17. html = 'Not-debounced AJAX request executed: ' + text_counter_1++ + ' times.'
  18. + ( val ? ' Text: ' + val : '' );
  19. $('#text-type-1').html( html );
  20. };
  21. // This function is debounced, and the new, debounced, function is bound to
  22. // the event. Note that in jQuery 1.4+ a reference to either the original or
  23. // debounced function can be passed to .unbind to unbind the function.
  24. function text_2() {
  25. var val = $(this).val(),
  26. html = 'Debounced AJAX request executed: ' + text_counter_2++ + ' times.'
  27. + ( val ? ' Text: ' + val : '' );
  28. $('#text-type-2').html( html );
  29. };
  30. // Bind the not-at-all debounced handler to the keyup event.
  31. $('input.text').keyup( text_1 );
  32. // Bind the debounced handler to the keyup event.
  33. $('input.text').keyup( $.debounce( 250, text_2 ) ); // This is the line you want!
  34. // Trigger the callbacks once to show some initial (zero) values.
  35. text_1();
  36. text_2();
  37. });
  38. <?
  39. $shell['script'] = ob_get_contents();
  40. ob_end_clean();
  41. // ========================================================================== //
  42. // HTML HEAD ADDITIONAL
  43. // ========================================================================== //
  44. ob_start();
  45. ?>
  46. <script type="text/javascript" src="../../jquery.ba-throttle-debounce.js"></script>
  47. <script type="text/javascript" language="javascript">
  48. <?= $shell['script']; ?>
  49. $(function(){
  50. // Syntax highlighter.
  51. SyntaxHighlighter.highlight();
  52. });
  53. </script>
  54. <style type="text/css" title="text/css">
  55. /*
  56. bg: #FDEBDC
  57. bg1: #FFD6AF
  58. bg2: #FFAB59
  59. orange: #FF7F00
  60. brown: #913D00
  61. lt. brown: #C4884F
  62. */
  63. #page {
  64. width: 700px;
  65. }
  66. input.text {
  67. width: 20em;
  68. border: 1px solid #000;
  69. padding: 0.3em;
  70. font-size: 120%;
  71. }
  72. </style>
  73. <?
  74. $shell['html_head'] = ob_get_contents();
  75. ob_end_clean();
  76. // ========================================================================== //
  77. // HTML BODY
  78. // ========================================================================== //
  79. ob_start();
  80. ?>
  81. <?= $shell['donate'] ?>
  82. <p>
  83. Using <a href="http://benalman.com/projects/jquery-throttle-debounce-plugin/">jQuery throttle / debounce</a>, you can pass a delay and function to <code>$.debounce</code> to get a new function, that when called repetitively, executes the original function just once per "bunch" of calls.
  84. </p>
  85. <p>
  86. This can be especially useful for rate limiting execution of handlers on events that will trigger AJAX requests. Just take a look at this example to see for yourself!
  87. </p>
  88. <h3>Typing into a textfield</h3>
  89. <p>Give this "pretend autocomplete" field a try. After typing a sentence you'll see why debouncing is a good idea!</p>
  90. <form action="" method="get">
  91. <input class="text" type="text" name="whatever">
  92. </form>
  93. <h3>The real-world simulation</h3>
  94. <p id="text-type-1"></p>
  95. <p id="text-type-2"></p>
  96. <p>
  97. <em>(No, there is no actual AJAX request happening here, but if there was, that not-debounced version would be killing your server)</em>
  98. </p>
  99. <h3>The code</h3>
  100. <div class="clear"></div>
  101. <pre class="brush:js">
  102. <?= htmlspecialchars( $shell['script'] ); ?>
  103. </pre>
  104. <?
  105. $shell['html_body'] = ob_get_contents();
  106. ob_end_clean();
  107. // ========================================================================== //
  108. // DRAW SHELL
  109. // ========================================================================== //
  110. draw_shell();
  111. ?>