[英]Wait to return from a javascript function until condition met
这是一个奇怪的问题。 我有一个客户端对象,我正在使用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;
};
我遇到的问题是我需要加载一些远程JSON资源,然后再返回新的'that'对象(它指示就绪客户端)。 数据以异步方式返回(显然),我设置布尔变量以指示每个远程资源何时返回。
我想过做以下事情:
return whenInitialized(function() { return that; });
whenInitialized函数返回两个布尔标志是否为真。 我将它与setInterval的组合一起使用,但我确信这不起作用。
非常感谢您的建议。
为了在异步操作成功后运行代码,您需要继续 。 它可以只是代码在操作完成时调用的回调。
像这样的东西:
var client = function(done) { // done is the callback
var that, remote_data, other_data;
// add public interface
that.doStuff = function(){...}
// wait for remote resources to load
var done1 = false, done2 = false;
var complete1 = function() { done1 = true; if (done2) done(); };
var complete2 = function() { done2 = true; if (done1) done(); };
remote_data = jsonRequest1(complete1);
other_data = jsonRequest2(complete2);
return that;
};
但是这些控制标志真的很烦人,并没有真正扩展。 一个更好的,声明性的方法是使用像jQuery deferreds这样的东西:
$.when(jsonRequest1(), jsonRequest2()).then(done);
您可以执行循环(可选择使用超时)以等待异步完成。 警告,这将(根据要求)阻止所有其他功能,并可能导致浏览器冻结,如果它需要太长时间。 但是,你真的应该找到一种异步方式来做你需要的而不是像这样阻塞。
var syncRequest = function(options) {
var is_finished = false;
var result;
var finished_callback = function(response) {
is_finished = true;
result = response.result;
}
ajax_request(options, finished_callback);
// timeout in 30 seconds
var timeout = (new Date()).getTime() + 30000;
while ( !is_finished ) {
if ( (new Date()).getTime() >= timeout ) {
alert('Request timed out');
}
// do nothing, just block and wait for the request to finish
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.