我试图在 javascript 中调用时间密集型函数之前向用户加载“加载”消息。
HTML:
<p id='foo'>Foo</p>
Javascript:
var foo = document.getElementById('foo');
function tellViewerLoading() {
// Tell the user that loading is occuring.
foo.innerHTML = 'loading...';
}
function someActionThatTakesALongTime() {
// Do some action that takes a long time.
var i = 0;
while(i < 100000) {i++; console.log(i);};
}
function domUpdateDelayExperiment() {
tellViewerLoading();
someActionThatTakesALongTime();
}
domUpdateDelayExperiment();
JSFiddle:http: //jsfiddle.net/johnhoffman/xDRVF/
我想要发生的是在tellViewerLoading()
调用后立即更新 DOM。
相反,DOM 似乎在someActionThatTakesALongTime()
完成运行后更新。此时,显示加载消息是没有用的。
如何告诉 javascript 在tellViewerLoading()
调用后立即更新 DOM?