Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Debounce for Inputs</h1>

<form id="search-form" role="search" aria-label="Site Search">
  
  <div>
    <label id="search-label" for="search-input">Search</label>
    <input 
      id="search-input"
      type="text"
      name="q"
      placeholder="Search..."
      required
    >
  </div>
  
  <div>
    <input type="checkbox" name="debounce-checkbox" id="debounce-checkbox">
    <label for="debounce-checkbox">Debounce</label>
  </div>
  
  <p>Search for "<code id="current-search-item-preview"></code>"</p>

<div>Search term length is <span id="search-length">0</span>.</div>
<div>Searched <span id="count">0</span> of times.</div>

  <button id="reset" >Reset</button>
</form>


              
            
!

CSS

              
                :root{
  --primary-color: #0c7acc;
}

body {
  font-family: system-ui;
  display: flex;
  flex-direction: column;
}

*{
  box-sizing: border-box;
}

body, html{
  height: 100%;
  
}

form{
  padding: 20px 0px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  max-width: 300px;
}

label#search-label{
  display: block;
  margin-bottom: 10px;
}

input[type="text"], button{
  width: 100%;
  padding: 10px 10px;
  border: 1px solid var( --primary-color );
}

button{
  background: var( --primary-color );
  border: 1px solid var( --primary-color );
  color: #fff;
  font-size: 15px;
}

input, button{
  border-radius: 5px;
}
              
            
!

JS

              
                import { debounce } from "https://esm.sh/throttle-debounce";

let count = 0;

document.addEventListener( 'DOMContentLoaded', function(){
  
  const form = document.getElementById('search-form');

  // Search Input Element
  const elSearchInput = document.getElementById( 'search-input' );
  
  // Debounce Checkbox Element
  const elDebounceCheckbox = document.getElementById( 'debounce-checkbox' );
  
  // Search term length preview
  const elSearchTermLengthInfo = document.getElementById( 'search-length' );

  // Search Term Processing
  const elSearchTerm = document.getElementById( 'current-search-item-preview' );
  
  // Count Element
  const elCount = document.getElementById( 'count' );

  elSearchInput.addEventListener( 'input', search );
  
  function fakeSearchAPICall( val ){
    if( val && val.length > 0 ){
      count++;
    } else {
      count = 0;
    }
    elCount.innerText = count;
  }
  
  const debounceFunc = debounce(
    500,
    (val) => {
      console.log( 'debounced' );
      fakeSearchAPICall(val);
    },
    { atBegin: false }
  );

  function search( e ){
    const inputVal = e.target.value;
    elSearchTerm.innerText = inputVal;
    
    if( elDebounceCheckbox.checked ){
      
      debounceFunc(inputVal);
      // fakeSearchAPICall(inputVal);
    } else {
      fakeSearchAPICall(inputVal);
    }
    
    // Search term length
    elSearchTermLengthInfo.innerText = inputVal.length;
    
  }
  
  // handle elReset click
  const elReset = document.getElementById( 'reset' );
  elReset.addEventListener( 'click', function(e){
    e.preventDefault();
    elSearchInput.value = '';
    count = 0;
    elCount.innerText = count;
    elSearchTermLengthInfo.innerText = e.target.value.length;
  } );
  
} )



              
            
!
999px

Console