Beezee Beezee - 2 months ago 16
jQuery Question

make something happen every 1 second?

Sorry if this seems like a noobie question. i am currently challenging myself by creating a pomodoro timer. I'm having trouble making the timer count down automatically.. so far my option thought is using a for loop although it does not count down at 59..58..57.. on the screen. My question is can i make it slower...

i currently have this if/else statement that will allow everything to go from 59 - 0 then reset to 59 again.

if (s >=0 && s < 10 ) {
$("#second").html("0"+s);
s--
} else if ( s <= 0) {
s = 59;
} else {
$("#second").html(s);
s--
}


sorry if this is quite a noob question

Answer Source
function runTimer() {
    let count = 59

    setInterval(() => {
        console.log(count)
        count--
        if (count < 1) count = 59
    }, 1000)
}

runTimer()