JSFiddle - React, Tailwind, and code Playground

by LyndseyB

HTML

<div id="timer"></div>

JavaScript

var timer = 10,  
    start = Date.now(),
    diff,
    seconds;

function countdown() {
    diff = timer - (((Date.now() - start) / 1000) | 0);
    seconds = (diff % 60) | 0;
    
    document.getElementById('timer').textContent = seconds;
    
    if (diff <= 0) {
        // add one second so that the count down starts at the full duration        
        start = Date.now() + 1000;
    } 
    
    if(diff === 0) {
        clearInterval(count);
    }
}

var count = setInterval(countdown, 100);