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

457
Views
Convertir segundos a milisegundos - javascript

Acabo de empezar a aprender javascript hoy y estoy tratando de averiguar cómo puedo convertir segundos a milisegundos.

Traté de encontrar algo que me ayudara, pero todo lo que encuentro está convirtiendo milisegundos en minutos u horas.

 let str = 'You must wait 140 seconds before changing hands'; let timer = 0; let num = str.match(/\d/g).join(""); timer = num; console.log(timer); setTimeout(() => { console.log('time done') }, timer);

Estoy tratando de extraer los números de la cadena y convertirlos en milisegundos para un setTimeout.

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

0

  • Aquí hay una expresión regular mejor /(\d+) seconds?/ - el ? significa que la s es opcional
  • 1 segundo son 1000 milisegundos como nos dice la palabra

 let str = 'You must wait 140 seconds before changing hands'; let timer = str.match(/(\d+) seconds?/)[1]*1000; // grab the captured number and multiply by 1000 console.log(timer) setTimeout(() => { console.log('time done') }, timer);

Aquí hay una cuenta regresiva

 let str = 'You must wait 10 seconds before changing hands'; const span = document.getElementById("timer"); let tId = setInterval(() => { let timeLeft = +str.match(/(\d+) seconds?/)[1]; // optional s on seconds if (timeLeft <= 1) { str = "Time's up"; clearInterval(tId); } else str = str.replace(/\d+ seconds?/,`${--timeLeft} second${timeLeft == 1 ? "" : "s"}`) span.innerHTML = str; }, 1000);
 <span id="timer"></span>

about 4 years ago · Juan Pablo Isaza Report

0

let str = 'You must wait 140 seconds before changing hands'; let seconds = /\d+/.exec(str)[0]; // milliseconds = seconds * 1000; const ms = seconds * 1000; setTimeout( () => {// doSomething}, 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!