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

158
Views
¿Por qué se ejecuta el valor predeterminado a pesar de que el número está establecido en 1?

 let spans = document.querySelector(`#spans`); let hrs = document.querySelector(`#hrs`); let mins = document.querySelector(`#mins`); let secs = document.querySelector(`#secs`); let start = document.querySelector(`#start`); let stop = document.querySelector(`#stop`); spans.style.fontSize = "10em"; let preFix = 0; let num = 1; let secspreNum= 0; let minspreNum = 0; let hrspreNum = 0; let myFunc = ()=> { setInterval(()=>{ switch (num){ case num===1: mins.innerHTML = `0` + num; num ===0; secs.innerHTML =`0`+ num++; default: console.log(`default test`) } } , 1000); }; start.addEventListener(`click`,myFunc)
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div id="spans"> <span id="hrs">00</span>:<span id="mins">00</span>:<span id="secs">00</span><br></div> <button id="start">Start</button> <button id="stop">Stop</button> <script src="go2.js"></script> </body> </html>

Hola, estoy tratando de hacer un cajero automático con cronómetro pero me quedé atascado en un problema. Intentando usar la declaración de cambio para configurar los minutos para sumar un número si los segundos llegan a 10. Pero antes de hacerlo, he estado probando esta declaración de cambio y, por alguna razón, ejecuta la declaración predeterminada en lugar de case. ¿Por qué es así? Establecí la variable num en 1 y aún ejecuta la predeterminada en lugar de num===1 . ¿Entendí mal algo? Gracias !

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

0

Aquí hay una versión de trabajo

Cambié su querySelector y los literales de plantilla innecesarios

 let spans = document.getElementById('spans'); let hrs = document.getElementById('hrs'); let mins = document.getElementById('mins'); let secs = document.getElementById('secs'); let start = document.getElementById('start'); let stop = document.getElementById('stop'); let tId; let num = 0; let myFunc = () => { clearInterval(tId); tId = setInterval(function() { const date = new Date(null); // alternatively calculate hh,mm,ss using %60 date.setSeconds(++num); const [hh, mm, ss] = date.toISOString().substr(11, 8).split(":"); hrs.textContent = hh; mins.textContent = mm; secs.textContent = ss; }, 1000) }; start.addEventListener('click', myFunc) stop.addEventListener('click', function() { clearInterval(tId) })
 #spans { font-size: 10em }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div id="spans"> <span id="hrs">00</span>:<span id="mins">00</span>:<span id="secs">00</span><br></div> <button id="start">Start</button> <button id="stop">Stop</button> <script src="go2.js"></script> </body> </html>

about 4 years ago · Juan Pablo Isaza Report

0

prueba esto:

 let myFunc = ()=> { setInterval(()=> { switch (num) { case 1: console.log(num) mins.innerHTML = `0` + num; secs.innerHTML =`0`+ (num++); break; default: console.log(`default test`) } } , 1000); };

case toma constantes y no expresiones afaik. También tiene que break o el código "caerá" a la siguiente condición.

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!