如何暂停JavaScript执行直到标志变为true?Javascript - 暂停执行,直到标志变为真
对于实施例中,我一个XML消息是这样的:
[...]
<action>
<resource update>id</resourceupdate>
</action>
<action>
<event>id1</event>
</action>
<action>
<event>id2</event>
</action>
<action>
<event>id3</event>
</action>
[...]
我希望事件节点只处理节点resourceupdate(这需要更多的时间来送达,后处理,因为它需要装载一个页的):
在JavaScript来处理具有一个迭代(每个此消息)我试着:
$(_response).find('ACTION').each(function() {
if (tagName=="RESOURCEUPDATE") {
ready = false;
//load the resource with selected id in an iframe
} else if (tagName=="EVENT") {
browserLoaded(); //the waiting function
eventhandler(); //consume the event
}
});
等待功能是:
function browserLoaded() {
if (!ready) {
setTimeout(browserLoaded(),1000);
}
}
,当iframe中加载准备var就成了真:
$(iframe).load(function() {
ready = true;
});
,但执行的时候我会抓住这个错误:
Maximum call stack size exceeded error
什么想法? 谢谢!
'setTimeout(browserLoaded(),1000);'执行你的函数。你只是想引用你的功能;删除'()',以便它'setTimeout(browserLoaded,1000);' – Sampson
感谢您的快速响应!好吧,没有更多的错误,但执行不会停止。 – jenjis