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

206
Views
¿Cómo controlar el formato de visualización de números usando Javascript?

Tengo una función Javascript que muestra un contador de tiempo como el siguiente: 0:0 and 0:1 . Pero en realidad quiero que se muestre así: 00:00 and 00:01 .

Aquí está el código:

 var interval = null; var time = 0 function increaseTimer() { // increase and print timer time++ console.log(new Date() + ' ' + 'timer: ' + parseInt(time / 60, 10) + ':' + parseInt(time % 60, 10)) // condition of a process if (time % 5 == 0) { // some process } // stop simulation after 30s if (time == 30) { clearInterval(interval) } }

Llamo a esta función con variable de intervalo: interval = setInterval(increaseTimer, 1000)

¿Qué debo cambiar en mi código para que funcione? Gracias por adelantado.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

var interval = null; var time = 0 function increaseTimer() { // increase and print timer time++ let prefix = parseInt(time / 60, 10) let postfix = parseInt(time % 60, 10) prefix = prefix <= 9 ? "0" + prefix.toString() : prefix.toString(); postfix = postfix <= 9 ? "0" + postfix.toString() : postfix.toString(); console.log(new Date() + ' ' + 'timer: ' + prefix + ':' + postfix ) // condition of a process if (time % 5 == 0) { // some process } // stop simulation after 30s if (time == 30) { clearInterval(interval) } }
over 4 years ago · Santiago Trujillo Report

0

Puedes simplificar el código de la siguiente manera:

 let interval = null, time = 0; function increaseTimer() { time++; // increase and print timer console.log(new Date()+' '+'timer: '+("0"+~~(time / 60)).slice(-2)+':'+("0"+time % 60).slice(-2)); if (time % 5 == 0) { // condition of a process // some process } if (time == 30) { // stop simulation after 30s clearInterval(interval) } }

over 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!