JSFiddle - React, Tailwind, and code Playground

HTML

<div id="counter">0</div>

JavaScript

// basic class implementation
function myCounter() {
    // privileged property for iteration
    this.i = 0;
    
    // privileged init method
    this.init();
}

// defining init method
myCounter.prototype.init = function () {
    setInterval(
        // we can also use bind() to assign the correct scope
        this.countUp.bind(this)
    , 500);
};

// defining the counter method
myCounter.prototype.countUp = function () {
    this.i++;
    document.getElementById('counter').innerHTML = this.i;
};

// create a new instance of our counter class
var counter = new myCounter();