我想停止一个函数,以便等待另一段代码的结束。Firefox OS中是否有一些同步方法,比如Java中的wait()和notify()?谢谢
发布于 2014-08-09 22:45:56
JavaScript没有这个概念,但通常使用回调(函数指针)。有点像Java中的匿名类。例如,我正在呼叫一台web服务器:
function callToWebServer(url, doneCallback) {
// do all kinds of magic, waiting for the web server to reply etc.
// when done:
doneCallback();
}现在通过以下方式使用它:
callToWebServer(function() {
// this is executed after the call to the web server succeeded
});
alert(1); // this is executed straight away我们永远不能等待,因为JS是一个单线程执行环境。所有代码都是异步编写的。
https://stackoverflow.com/questions/25200690
复制相似问题