JSFiddle - React, Tailwind, and code Playground

by coiscir

HTML

<div id="display"></div>

<button type="button" id="start">Start</button>
<button type="button" id="stop">Stop</button>

JavaScript

// ref: http://stackoverflow.com/q/18961878/15031

var display = document.getElementById('display');
var starter = document.getElementById('start');
var stopper = document.getElementById('stop');

var interval, counter = 0;

function updateDisplay() {
    if (display.firstChild)
        display.removeChild(display.firstChild);
    
    display.appendChild(document.createTextNode(counter++));
}

function stopCounting() {
    clearInterval(interval);
}

function startCounting() {
    stopCounting(); // avoid multiple intervals at once
    
    interval = setInterval(updateDisplay, 1);
}

starter.addEventListener('click', startCounting, false);
stopper.addEventListener('click', stopCounting, false);

// initial display
updateDisplay();