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

66
Views
Mientras que el bucle no se ejecuta correctamente si las condiciones son verdaderas

Mi variable normalWeek es igual a un cierto período de tiempo, si este período de tiempo es verdadero, entonces se ejecutará la primera condición; si no lo es, el ciclo while ejecutará la segunda condición hasta que la variable normalWeek se vuelva verdadera. Pero el problema es que cuando ejecuto el código, la primera condición que el ciclo encuentra verdadera será la única que se ejecutará incluso después de que otra condición se vuelva verdadera.

Por ejemplo, la variable normalWeek es verdadera si son las 6 a. m., pero el ciclo continuará ejecutando esta misma condición incluso después de que no sean las 6 a. m., básicamente ignora la otra condición.

 var date = new Date(); var hour = date.getHours(); var day = date.getDay(); var minute = date.getMinutes(); function goodSignal() { console.log(b, 'Good Signal'); } function badSignal() { console.log(b, 'Bad Signal'); } function sleep(miliseconds) { var currentTime = new Date().getTime(); while (currentTime + miliseconds >= new Date().getTime()) { } } let b = 0; let normalWeek = (hour === 6 || hour === 10) && (day > 0 && day < 6); while (b === 0) { console.log('Testing Started...'); while (normalWeek === true) { b++; goodSignal(); sleep(10000); continue; } while (normalWeek === false) { b++; badSignal(); sleep(10000); continue; } }

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

0

Hola Iago: Echa un vistazo al siguiente código: Incluí explicaciones en los comentarios. Ilustra cómo puede verificar una "semana normal" usando una nueva fecha cada vez en lugar de reutilizar la misma fecha. Hice lo mejor que pude para interpretar el objetivo de tu pregunta, pero si quieres aclarar algo, házmelo saber en un comentario y lo revisaré.

 // This will compute a new date every time instead of using the same date each time function isNormalWeek () { const date = new Date(); const day = date.getDay(); const hours = date.getHours(); // return (hours === 6 || hours === 10) && (day > 0 && day < 6); // Just for this example, let's say it's normal if the seconds are even // instead of waiting for 06:00-06:59 or 10:00-10:59 on a weekday const seconds = date.getSeconds(); return seconds % 2 === 0; // seconds are even } // This is preferable to running a loop endlessly for no reason function sleep (ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function main () { function goodSignal () { console.log(b, 'Good Signal'); } function badSignal () { console.log(b, 'Bad Signal'); } let b = 0; // Always true, loop forever while (true) { b += 1; // Invoke one function if the week is "normal", otherwise invoke the other isNormalWeek() ? goodSignal() : badSignal(); // await sleep(10_000); // Just for this example, let's run the loop again after waiting one second // instead of waiting ten seconds await sleep(1_000); } } main();

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!