Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
How to throttle checkbox check & uncheck actions

May I know how can I throttle checkbox check & uncheck actions to prevent users click checkbox and change the check & uncheck too frequently? How to use the throttle function to control the check & uncheck action? For example, we only change check & uncheck at most every 2000ms even user click 100 times.
reference: https://towardsdev.com/debouncing-and-throttling-in-javascript-8862efe2b563

const throttle = (func, limit) => {
  let inThrottle;
  return function() {
    if (!inThrottle) {
      func();
      inThrottle = true;
      setTimeout(() => inThrottle = false, limit);
    }
  }
}
<input type = "checkbox" id = "checkbox1">
<label for="checkAll">Checkbox 1</label>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use e.preventDefault() to control the check/uncheck action.

const throttle = (func, limit) => {
  let inThrottle;
  
  return function(e) {
    if (inThrottle) {
      return e.preventDefault();  // Prevents check/uncheck of the input
    }
    
    func();
    inThrottle = true;
    setTimeout(() => inThrottle = false, limit);
  }
}

function clickHandler() {}

document.getElementById("checkbox1").addEventListener("click", throttle(clickHandler, 2000))
<input type = "checkbox" id = "checkbox1">
<label for="checkAll">Checkbox 1 (Can only be changed every 2s)</label>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Date.now() and measure the milliseconds.

const c = document.getElementById("c");

const throttleMs = 500; // how much you'd like to throttle in milliseconds
let lastClick = Date.now();
c.addEventListener("change", e => {
  // check if last date - current date is less than 500ms
  if (Date.now() - lastClick < throttleMs) {
    c.checked = !c.checked; // e.preventDefault() doesn't work for checkboxes
  }
  lastClick = Date.now();
});
<input type="checkbox" id="c" />

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda