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

116
Views
Display countdown from 10 to 1 on the screen and then display Happy Morning on the screen. Tried using promises

/Result got printed. But don't understand why its showing [[PromiseState]]: "rejected", it should be "fulfilled", what was the mistake ,i made in the code??/

let counterElement = document.querySelector(".counter");

const promiseCheck = new Promise((resolve, reject) => {
  let Timer = setInterval(() => {
    var runner = parseInt(document.querySelector(".counter").innerText);
    counterElement.innerText = runner - 1;
    if (runner === 0) {
     clearInterval(Timer);
     resolve((counterElement.innerText = "Happy Morning"));
    }else{
      reject("wht going wrong")
    }
    // console.log(runner)
  }, 1000);
});
console.log(promiseCheck);


promiseCheck
  .then((data) => console.log(data))
  .catch((errData) => console.log(errData));

index.html
   <h1 class="counter">10</h1>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use requestAnimationFrame to achieve this result cleanly. It works like setTimeout and setInterval (which could also be employed on those lines).

const timer = document.getElementById("time");
var startTime = null;
var endTime = null;

function changeTimer() {
  if (startTime === null) {
    startTime = Date.now();
    /* Set up the start time to be 11 seconds (11,000 ms)
       in the future so it displays 10 for a whole second */
    endTime = startTime + 11000;
    requestAnimationFrame(changeTimer);
    return;
  }
  let nValue = Date.now();
  let tValue = (nValue < endTime) ? endTime - nValue : 0;
  let mtValue = tValue / 1000;
  let mtiValue = parseInt(mtValue);
  if (mtiValue < 1) {
    timer.innerText = "Happy Morning";
    return;
  }
  timer.innerText = mtiValue;
  requestAnimationFrame(changeTimer);
  return;
}
changeTimer();
<h1 class="counter" id="time">10</h1>

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!