function simulateComplexOperation(sleepDuration) {
var now = new Date().getTime();
while (new Date().getTime() < now + sleepDuration) { /* do nothing */ }
}
function testFunction() {
document.getElementById('panel1').style.display = 'none';
console.log('before');
simulateComplexOperation(2000);
console.log('after');
}
&#13;
<div id='panel1'>
text to hidden
</div>
<button onclick="testFunction()">Hide</button>
&#13;
(jsFiddle)
这是时间表:
为什么不是:
有没有办法强制将样式更改操作作为第一个?
答案 0 :(得分:0)
您最好使用setTimeout。但这是由浏览器调度代码引起的。
function simulateComplexOperation(sleepDuration) {
var now = new Date().getTime();
while (new Date().getTime() < now + sleepDuration) { /* do nothing */ }
}
function testFunction() {
document.getElementById('panel1').style.display = 'none';
console.log('before');
setTimeout(() => {
console.log('after');
}, 2000);
}
<div id='panel1'>
text to hidden
</div>
<button onclick="testFunction()">Hide</button>