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

205
Views
Is async callback happening with setInterval() here?

I'm making a rookie second stopwatch here only using setInterval.

I tried to visually stop the stopwatch from counting up by artificially overriding the innerHTML of the timer display, which I know is pretty dumb.

When I un-pause the stopwatch, the display increments more than one count every second, which is quite interesting. I'd like to know why this happens - is it because the countedTime still runs in the event queue, while the startStopwatch() refreshes the display every second?

Is this what they call an async callback?

var countedTime = 0
var paused

// specify HTML areas for components
const displayArea = document.querySelector("#timer-display")
const countUpButton = document.querySelector("#count-up-button")
const pauseButton = document.querySelector("#pause-button")


// increment timer by 1 second 

function countUp() {
    if (paused === false) {
    countedTime += 1
    displayArea.innerHTML = countedTime
    }
}

// function to begin counting up

function startStopwatch() {
    paused = false
    setInterval(countUp, 1000)
}

countUpButton.addEventListener("click", startStopwatch);

// add pause button

function pauseStopwatch() {
    paused = true
}

pauseButton.addEventListener("click", pauseStopwatch)
about 4 years ago · Juan Pablo Isaza
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!