I want to hide an element, and display a new element after one second,
here's the simplified code :
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
function loadNewSubmenu()
{
document.getElementById("content-1").className = "hide-part";
sleep(1000);
document.getElementById("content-2").className = "show-part";
}
.hide-part{
display : none;
}
<button id="content-1" class="show-part">B1</button>
<button id="content-2" class="hide-part">B2</button>
<button onclick="loadNewSubmenu()">click me</button>
What I expected :
- Hide the element
#content-1
- Wait one second
- Show the element
#content-2
But this is what i got :
- One second wait
- Element
#content-1
changed - Element
#content-2
changed
I know that I can use setTimeout
to fix it, but what's the reason behind this behavior?