I have a button. onClick the button fires a JS function. In this function on the very first line of code I change the button's HTML using jQuery. Then the function takes a couple seconds. The button HTML only changes (in browser) after the complete function is finished.
https://jsfiddle.net/wqps1r0k/
The code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<button class="btn btn-primary" onclick="setToDisabled(this)">Toggle</button>
<script type="text/javascript">
function setToDisabled(btn) {
//log
console.log('start');
//how to force the browser to show this text immediately?
$(btn).html("TEXT CHANGED ON START OF FUNCTION");
//create a 2 seconds delay
var d1 = new Date();
var t1 = d1.getTime();
var keeprunning = true;
while(keeprunning) {
var d2 = new Date();
var t2 = d2.getTime();
var dif = t2 - t1;
if((dif / 1000) > 2) keeprunning = false;
}
//log
console.log('done');
}
</script>
In this example, would it be possible to change the button text at the beginning of the flow and see this in my browser immediately? For example to change the text to 'Loading...'.
If I inspect the button you can see the text changing directly, but it is only visible after the function completes.
Ps. I use chrome