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

255
Views
¿Es esta una forma de agregar 1 y luego, después de un segundo, agregar 2 y luego 3, etc.?

Lo que estoy buscando sería esencialmente un comando "++" a "++" usando un script java nativo. El programa simplemente ejecuta una animación para un número dado en el que la idea de la animación es que suma 1 después de un segundo, dos después de dos segundos y continúa de la misma manera hasta que se detiene la animación.

 var counter = 10; var animationOn = false; var counterAnimation; var plusOne; function updateCounter() { //update the counter value var plusOne = counter++; for (var i = 1; i = < 100000000;) { } //show the counter var counterSpan = document.getElementById("counterHolder"); counterSpan.innerHTML = plusOne; } function startCounterAnimation() { if (animationOn == false) { animationOn == true; counterAnimation = setInterval(updateCounter, 1000); } } function stopCounterAnimation() { if (animationOn == true) { animationOn == false; } }
 <!DOCTYPE html> <html> <head> </head> <body> <button onclick="startCounterAnimation();"> Start counter animation </button> <button onclick="stopCounterAnimation();"> Stop counter animation </button> <span id="counterHolder">6931418</span> </body> </html>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

  1. No necesitas el bucle for .
  2. Debe asignar a la variable global plusOne , no declarar una variable local en la función.
  3. Debe agregarle counter++ , no asignarlo directamente.
  4. Inicialice plusOne a partir del número que ya se encuentra en el intervalo de salida.
  5. Dado que sus intervalos de tiempo cambian entre cada actualización, no puede usar setInterval() . Use setTimeout() para hacer un tiempo de espera diferente cada vez.
  6. Use = , no == , para asignar a la variable animationOn .

 var counter; var animationOn = false; var counterAnimation; var plusOne = parseInt(document.getElementById("counterHolder").innerHTML); function updateCounter() { //update the counter value plusOne += counter++; //show the counter var counterSpan = document.getElementById("counterHolder"); counterSpan.innerHTML = plusOne; counterAnimation = setTimeout(updateCounter, counter * 1000); } function startCounterAnimation() { if (!animationOn) { animationOn = true; counter = 1; counterAnimation = setTimeout(updateCounter, 1000 * counter); } } function stopCounterAnimation() { if (animationOn) { animationOn = false; clearTimeout(counterAnimation); } }
 <!DOCTYPE html> <html> <head> </head> <body> <button onclick="startCounterAnimation();"> Start counter animation </button> <button onclick="stopCounterAnimation();"> Stop counter animation </button> <span id="counterHolder">6931418</span> </body> </html>

about 4 years ago · Juan Pablo Isaza Report

0

No puede definir var onePlus en dos lugares, sino definirlo una vez y asignarle un valor, var tiene un alcance amplio

También agregue cualquier lógica que necesite en el ciclo for y coloque onePlus ++ y el counter como partes del ciclo for

Tal como está ahora, espera usar el counter tanto fuera del bucle for como en la parte media del bucle for, lo que parece redundante

Pruebe algo como (dejando los nombres de las variables como están):

 var counter = 1000 for (var plusOne = 1; plusOne < counter; plusOne++) { //Your logic in here await sleep(1000); } function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }
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!