<p>Second click gets processed BEFORE the setTimeout(..,0) in the first click.
<p/>
<p>
<input type='button' id='mybutton1' class='mybutton' onclick='clickHandler(this);' />
</p>
<p>Second click gets processed AFTER the setTimeout(..,0) in the first click.
<p/>
<p>
<input type='button' id='mybutton2' class='mybutton' onclick='clickHandler(this);' />
</p>
<p>
<input type='button' onclick='reset()' value='RESET' />
</p>
JavaScript
function spinWait(MILLIS) {
var end = Date.now() + MILLIS;
console.log('Gonna wait ' + MILLIS + 'ms');
while (Date.now() < end);
console.log('Done waiting');
}
window.reset = function () {
$('.mybutton').removeAttr('disabled');
$('.mybutton').removeAttr('called');
$('.mybutton').val('click TWICE');
}
window.clickHandler = function (target) {
console.log(target);
var el = $(target);
var button1 = el.attr('id') === 'mybutton1';
if (!el.attr('called')) {
el.attr('called',true);
if(button1) spinWait(1000);
setTimeout(function () {
el.val('FROM FIRST CLICK');
}, 0);
if(!button1) spinWait(1000);
} else {
el.attr('disabled', 'disabled');
el.val('FROM SECOND CLICK');
}
};
window.reset();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!