Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Una llamada asíncrona por botón. ¿Cómo manejo a los usuarios que hacen clic en varios botones en rápida sucesión?

Tengo una página de administración con varios botones que hacen llamadas axios para cambiar el contenido. Cada botón hace una llamada diferente.

Cuando el usuario hace clic en el botón 1, los datos se recuperan del servidor para mostrarlos en pantalla. Pero si el usuario hace clic en el botón 1, luego en el 2 en rápida sucesión, la interfaz de usuario muestra los datos del 1 y luego del 2.

Esto no es necesariamente un gran problema, pero me gustaría conocer el mejor enfoque, ya que me causa problemas al animar los datos con gsap (ver más abajo).

¿Es el mejor enfoque para probar y usar tokens para cancelar la solicitud de axios?

Codepen: ejemplo sin animación Un ejemplo ligeramente diferente con animación

JS

 document.body.addEventListener('click', async e => { const item1 = e.target.closest('.item1'); const item2 = e.target.closest('.item2'); const item3 = e.target.closest('.item3'); if(item1) { await dummyAsync(); changeContent(1); } if(item2) { await dummyAsync(); changeContent(2); } if(item3) { await dummyAsync(); changeContent(3); } }); function dummyAsync() { return new Promise((resolve, reject) => { setTimeout(() => resolve(), 2000); }); } function changeContent(item) { // Clear previous content const wrapper = document.querySelector('.content-wrapper'); while(wrapper.firstChild) wrapper.removeChild(wrapper.firstChild); const dummyTable = `<div class="table">This is table number: ${item}</div>`; // Add table wrapper.insertAdjacentHTML('afterbegin', dummyTable); };

HTML y CSS

 <div class="menu"> <div class="item item1">Item1</div> <div class="item item2">Item2</div> <div class="item item3">Item3</div> </div> <div class="content-wrapper"> <div class="content">Content</div> </div> .menu { display: flex; justify-content: space-evenly; width: 20rem; border: 1px solid lightgrey; } .item { cursor: pointer; padding: 1rem; z-index: 2; } .content-wrapper { display: flex; justify-content: center; align-items: center; margin-top: 2rem; border: 1px solid lightgrey; height: 20rem; } .table { display: flex; justify-content: center; align-items: center; width: 25rem; height: 10rem; border: 1px dotted grey; }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Querría usar un rebote para esto, esto limita la cantidad de llamadas a funciones que se realizan en un cierto período de tiempo.

Aquí hay un ejemplo usando su código: https://codepen.io/rdenton93/pen/MWOoBVK

 import lodashDebounce from "https://cdn.skypack.dev/lodash.debounce@4.0.8"; document.body.addEventListener('click', async e => { const item1 = e.target.closest('.item1'); const item2 = e.target.closest('.item2'); const item3 = e.target.closest('.item3'); if(item1) { await dummyAsync(); changeContent(1); } if(item2) { await dummyAsync(); changeContent(2); } if(item3) { await dummyAsync(); changeContent(3); } }) function dummyAsync() { return new Promise((resolve, reject) => { setTimeout(() => resolve(), 2000); }); } const changeContent = lodashDebounce((item) => { // Clear previous content const wrapper = document.querySelector('.content-wrapper'); while(wrapper.firstChild) wrapper.removeChild(wrapper.firstChild); const dummyTable = `<div class="table">This is table number: ${item}</div>`; // Add table wrapper.insertAdjacentHTML('afterbegin', dummyTable); },1* 1000); // only make one request per second
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!