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

246
Views
Cómo permitir que el usuario solo llame a mi función uno

Estoy tratando de mover un cuadro permitiendo que el usuario haga clic en un botón , pero solo quiero que el usuario pueda llamar a la función una vez en 'javascript'.

 function myMove() { let id = null; let id2 = null; const elem = document.getElementById("animate"); let pos = 0; clearInterval(id); id = setInterval(frame, 1); function frame() { if (pos == 250) { clearInterval(id); id2 = setInterval(backframe, 1); } else { pos++; elem.style.top = pos + "px"; elem.style.left = pos + "px"; } } function backframe() { if (pos == 0) { clearInterval(id2); id = setInterval(frame, 1); } else { pos--; elem.style.top = pos + "px"; elem.style.left = pos + "px"; } } }
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Para agregar un detector de eventos, que solo se activa una vez, addEventListener puede aceptar 3 argumentos: tipo, detector y opciones .

options es un objeto que tiene una propiedad llamada once , la ha establecido en true .

Ex:

 button.addEventListener('click', myMove, { once: true })
about 4 years ago · Santiago Trujillo Report

0

Aplique estas modificaciones a su código:

 let wasUsed = false; function myMove() { if (wasUsed) return; // stops the function if it has ran before wasUsed = true; // the rest of your function doesn't change: let id = null; let id2 = null; ... }
about 4 years ago · Santiago Trujillo 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!