JavaScript 对样式的更改被延迟
我遇到了一个让我发疯的小问题,我欢迎任何关于原因的想法。此时此刻,我感觉我只是在原地打转。
我有以下代码:
function JSsortTable(phase) {
dynaLoadingDivShow();
createSortArray();
dataArr = do2DArraySort(dataArr, orderList, orderDir);
sortArrayToRs();
dynaListTable.tableControl.refreshTableViaObjects(rsDynaList, colObjs);
dynaLoadingDivHide();
}
function dynaLoadingDivShow() {
document.getElementById('dynaReportGuiWorking').style.display = 'block';
document.getElementById('dynaReportGuiWorking').style.visibility = 'visible';
}
function dynaLoadingDivHide() {
document.getElementById('dynaReportGuiWorking').style.display = 'none';
document.getElementById('dynaReportGuiWorking').style.visibility = 'hidden';
}
<div style="visibility:hidden; display:none; z-index:25;" class="tableControlHeader" id="dynaReportGuiWorking">
Working...
</div>
我将 JSsortTable 称为 onclick 事件。当我按原样运行上面的代码时,我从未看到有问题的 div。 JSsortTable 函数需要大约 800-2500 毫秒才能运行,因此我尝试了 10 多次却错过了它,这是极不可能的。如果我将 div 的样式更改为开始可见,那么它将保持可见,直到 JSsortTable 完成运行然后消失;完全符合预期。所以我认为问题出在 dynaLoadingDivShow 中。
现在,我尝试删除 dynaLoadingDivHide 以查看会发生什么,并发现了完全意外的情况。当 JSsortTable 函数触发时,div 将不会出现。相反,在运行所有其他代码后,当 JSsortTable 完成时,div 就会变得可见。就好像 IE(版本 8)正在保存对 DOM 的所有更改,然后等到最后才绘制它们。显然,这不是期望的行为。
有人对此有什么想法吗?我只允许使用 IE 工作,所以我没有在其他浏览器上尝试过。我有足够的 CSS/JS 知识,很危险,但我还不是专家。 ;)
谢谢!
I'm running into a little problem that's driving me crazy, and I'd welcome any thoughts as to the cause. At this point I feel like I'm just going 'round in circles.
I have the following code:
function JSsortTable(phase) {
dynaLoadingDivShow();
createSortArray();
dataArr = do2DArraySort(dataArr, orderList, orderDir);
sortArrayToRs();
dynaListTable.tableControl.refreshTableViaObjects(rsDynaList, colObjs);
dynaLoadingDivHide();
}
function dynaLoadingDivShow() {
document.getElementById('dynaReportGuiWorking').style.display = 'block';
document.getElementById('dynaReportGuiWorking').style.visibility = 'visible';
}
function dynaLoadingDivHide() {
document.getElementById('dynaReportGuiWorking').style.display = 'none';
document.getElementById('dynaReportGuiWorking').style.visibility = 'hidden';
}
<div style="visibility:hidden; display:none; z-index:25;" class="tableControlHeader" id="dynaReportGuiWorking">
Working...
</div>
I call JSsortTable as an onclick event. When I run the above code as is, I never see the div in question. The JSsortTable function takes some 800-2500 ms to run so it's highly unlikely I just missed it the 10+ times I tried. If I change the style of the div to start out visible, then it will remain visible until after JSsortTable has finished running and then disappear; exactly as expected. So I figured the problem was in dynaLoadingDivShow.
Now, I tried removing dynaLoadingDivHide to see what would happen and found something completely unexpected. The div will not appear when you the JSsortTable function fires. Instead, after all the other code has been run, when JSsortTable finishes, the div becomes visible. It's alomst as though IE (version 8) is saving up all the changes to the DOM and then waiting until the end to paint them. This is, obviously, not the desired behavior.
Anyone have any thoughts on this? I'm only allowed to have IE at work so I haven't tried this on other browsers. I have enough CSS/JS knowledge to be dangerous, but am by no means an expert yet. ;)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用超时:
请注意,我删除了参数阶段,因为函数中未使用它。如果您确实需要该参数,那么您需要将超时修改为
并将参数添加到 JOrtTableWork
You'll need to use a timeout:
Note that I took out the parameter phase because it's not used in the function. If you do need the parameter then you'll need to modify the timeout as
and also add the parameter to JSortTableWork