JSFiddle - React, Tailwind, and code Playground

by Kathirvel Shanmugasundaram

HTML

<input type="button" value="Start count!" onclick="timedCount();" />
<input type="text" id="txt" />

JavaScript

var c = 0;
var t;

function timedCount() {
    document.getElementById('txt').value = c;
    c = c + 1;
    if (c == 5) {
        clearTimeout(t);
    }
    t = setTimeout(function () {
        timedCount()
    }, 1000);
}