【发布时间】:2011-10-31 18:24:25
【问题描述】:
在 javascript 中是否有一个事件,我可以绑定某种监听器来告诉我所有 javascript/jQuery/Ajax 何时在页面上执行完毕?在执行开始和我需要侦听器“侦听”的时间之间,页面不会加载/卸载/重新加载等,因此这些事件不起作用。该页面实际上没有做任何事情。单击该按钮并触发一些包含对 Web 服务的 Ajax 调用的 javascript 函数。毕竟都完成了,我想改变window.location。但是在我的情况下,window.location 在网络服务完成之前就发生了变化。
目前使用 setTimeout 来实现这一点,但有时代码需要比正常运行更多的时间,有时 window.location 会在所有其他 javascript 完成之前触发。简单地说
<input type = "button"... onclick="doThis();";
function doThis() {
try{
//Contains AJAX calls to web services which is mainly what screws up my timing since it may still be trying to execute stuff when the redirect statement happens
}
catch (e) {
}
//Currently doing setTimeout(redirect, 10000);
//Would like to simply detect when all of the above is done and then redirect.
}
编辑:遗漏了重要信息。 AJAX 调用在 for 循环中。变量和成功回调的使用对我来说并没有那么好,因为当我的成功回调执行时,我的变量在 for 循环中采用了新值。
【问题讨论】:
-
回调是你的朋友;)
标签: javascript ajax