JSFiddle - React, Tailwind, and code Playground
HTML
<table>
<tr>
<td rowspan="2">
<img src="http://img2.wikia.nocookie.net/__cb20130308043142/minecraftpocketedition/images/6/6d/Pickaxe_1.jpeg" id="imgPickaxe" />
</td>
<td>Total rocks:</td>
<td><label id="lblTotalRocks">0</label></td>
</tr>
<tr>
<td>Rocks per second:</td>
<td><label id="lblRocksPerSec">0</label></td>
</tr>
<tr>
<td><button id="btnBuyShovel" type="button">Buy Shovel<br /><small>(-5 rocks)</small></button></td>
<td>Shovels:</td>
<td><label id="lblShovels">0</label></td>
</tr>
<tr>
<td colspan="3">
<button id="btnStart" type="button" style="width: 30%;">Start</button>
<button id="btnStop" type="button" style="width: 30%;">Stop</button>
<button id="btnReset" type="button" style="width: 30%;">Reset</button>
</td>
</tr>
</table>
JavaScript
HTMLElement.prototype.on = function(eventName, func) {
var fn = 'on' + eventName,
old_fn = this[fn],
new_fn = typeof old_fn === 'undefined' || !old_fn || !old_fn.apply ?
function(e) { func.apply(this, [e]); }.bind(this) :
function(e) { old_fn([e]); if (e.cancelled) { return; }; new_fn.apply(this, [e]); }.bind(this);
this[fn] = new_fn;
};
function Miner(options) {
var addition = 0.1, props = {
totalRocks: 0,
totalShovels: 0
}, fnPropCreate = function(prop, value) {
Object.defineProperty(this, prop, {
get: function() {
return props[prop];
},
set: function(val) {
props[prop] = val;
this.update(prop);
}
});
}, fnSetLabel = function(element, value, decimals) {
var el = document.getElementById(element);
if (el) {
if (typeof decimals === 'undefined' || !decimals) {
el.innerHTML = Math.floor(value);
} else {
var divider = 1.0;
for (var i = 0; i < decimals; i++) {
divider *= 10.0;
}
el.innerHTML = (Math.floor(value * divider) / divider);
}
}
}, interval, running = false;
for (var prop in props) {
if (!props.hasOwnProperty(prop)) {
continue;
}
fnPropCreate.apply(this, [prop, props[prop]]);
}
this.update = function(prop) {
console.log(prop + ' updated to ' + this[prop]);
if (prop === 'totalRocks') {
fnSetLabel('lblTotalRocks', this.totalRocks, 0);
return;
}
if (prop === 'totalShovels') {
fnSetLabel('lblShovels', this.totalShovels, 0);
fnSetLabel('lblRocksPerSec', this.totalShovels * addition, 2);
return;
}
}
this.autoIncrease = function() {
...