根据this question ,当我们设置window.location时,javascript将“停止”执行或变成竞争条件。
有时我们需要在 WebView 内多次触发 window.location = SOMESCH://xxx
才能将“通知”发送回我们的应用程序。例如,设置 window.location = myapp://loginButtonEnabled?e=1
来告诉应用程序用户已填写了一些必要信息并且可以开始登录。似乎不可能做这样的事情:
function(){
window.location = myapp://loginButtonEnable?e=1;
window.location = myapp://hideHintView;
.....
window.location = myapp://theLastThing;
}
只有最后一个 window.location = myapp://theLastThing
将被触发,然后 Javascript 的执行将停止(尽管我们通过在 webView 中返回 NO 来停止应用程序中的重定向) :shouldStartLoadWithRequest:navigationType:
)。
我发现有趣的是,PhoneGap 通过使用调度队列使这成为可能,但我仍然不明白它为什么起作用,有人知道其中的技巧吗??
顺便说一句,有没有一种简单的方法可以在设置位置后“恢复”执行?这会比使用操作队列好得多。
最佳答案
您需要让事件循环有机会响应每次位置更改。执行此操作的典型方法是使用具有小/零延迟的 setTimeout
,这具有将执行移至下一个事件循环标记的效果。尝试这样的事情:
var q=[];
function dequeue() {
window.location='myapp://'+q.shift();
if (q.length>0) setTimeout(dequeue,0);
}
function notifyApp(cmd) {
q.push(cmd);
if (q.length==1) setTimeout(dequeue,0);
}
notifyApp('loginButtonEnable?e=1');
notifyApp('hideHintView');
notifyApp('theLastThing');
对于 javascript 执行停止,设置 window.location
不应该产生这种效果,除非它实际上导致页面更改 - 也许尝试使用上面的技术,看看你的 javascript 在最后一个之后是否继续notifyApp()
调用。
编辑:解决此问题的另一种方法是创建临时 iframe,而不是更改当前页面的位置 - 例如参见 Triggering shouldStartLoadWithRequest with multiple window.location.href calls
关于javascript - PhoneGap 如何在更改 window.location/document.location 后保持 Javascript 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12248913/