JSFiddle - React, Tailwind, and code Playground
by dirtyd77
HTML
<button type="button" id="click" onclick="cd(this, 5)">Get a banana</button>
JavaScript
var oldValue = null;
function cd(button, x) {
var ms = x * 1000;
// if you dont do this, you will get "Get a banana [5] [4] etc..."
if(!oldValue){
oldValue = button.innerHTML;
}
button.setAttribute('disabled', true);
button.innerHTML = oldValue + " [" + x + "]";
setTimeout(function(){
// decrease x
x--;
if(x > 0){
cd(button, x);
}else{
// remove attribute
button.removeAttribute('disabled');
// change back to oldValue
button.innerHTML = oldValue;
//reset oldValue
oldValue = null;
}
}, 1000);
}