这是一个奇怪的问题.我有一个客户端对象,我正在使用Crockford式公共/私人成员构建:
var client = function() {
var that, remote_data, other_data;
// add public interface
that.doStuff = function(){...}
// wait for remote resources to load
remote_data = jsonRequest1();
other_data = jsonRequest2();
return that;
};
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我需要加载一些远程JSON资源,然后再返回新的'that'对象(它指示就绪客户端).数据以异步方式返回(显然),我设置布尔变量以指示每个远程资源何时返回.
我想过做以下事情:
return whenInitialized(function() { return that; });
Run Code Online (Sandbox Code Playgroud)
whenInitialized函数返回两个布尔标志是否为真.我将它与setInterval的组合一起使用,但我确信这不起作用.
非常感谢您的建议.