<main>
<div class="input-container">
<label for="name">Input:</label><br>
<input type="text" id="name">
<p>onInput: <span id="input-value"></span></p>
<p>debouncedInput: <span id="debounce-value"></span>
</p>
</div>
</main>
<footer>
<p>
Pen by <a href="https://www.jemimaabu.com" target="_blank">Jemima Abu</a><span style="color: #D11E15"> ♥</span>
</p>
</footer>
body {
margin: 0;
background-color: #f4df4f;
font-family: system-ui, Roboto, Arial, sans-serif;
font-size: 1.4em;
line-height: 1.6;
color: #242424;
}
* {
box-sizing: border-box;
}
main {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.input-container {
width: 400px;
padding: 1em;
}
input {
width: 100%;
padding: 0.5em;
}
p {
word-break: break-all;
margin: 0.5em 0;
}
#input-value {
color: #ba0e51;
}
#debounce-value {
color: #0b9713;
}
footer {
text-align: center;
padding: 0.5rem 0;
background-color: #eaeaea;
}
footer p {
font-size: 0.75rem;
margin: 0.25rem 0;
color: #221133;
}
footer a {
text-decoration: none;
color: inherit;
}
var input = document.getElementById('name');
var inputValue = document.getElementById('input-value');
var debounceValue = document.getElementById('debounce-value');
const updateInputValue = () => {
inputValue.innerHTML = input.value;
}
const updateDebounceValue = () => {
debounceValue.innerHTML = input.value;
}
let debounceTimer;
const debounce = (callback, time) => {
window.clearTimeout(debounceTimer);
debounceTimer = window.setTimeout(callback, time);
};
input.addEventListener(
"input",
() => {
updateInputValue();
debounce(updateDebounceValue, 500)
},
false
);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.