長時間実行される HTTP 要求を受信した後にのみレンダリングできるクラスがありChatRoom
ます (1 秒または 30 秒かかる場合があります)。したがってChatRoom.json
、 null でなくなるまでレンダリングを遅らせる必要があります。
以下のコードでは、Closure Library のgoog.async.ConditionalDelay
. それは機能しますが、これを行うためのより良い方法はありますか (おそらく Closure Library を必要とせずに)?
ChatRoom.prototype.json = null; // received after a long-running HTTP request.
ChatRoom.prototype.render = function() {
var thisChatRoom = this;
function onReady() {
console.log("Received JSON", thisChatRoom.json);
// Do rendering...
}
function onFailure() {
alert('Sorry, an error occurred. The chat room couldn\'t open');
}
function isReady() {
if (thisChatRoom.json != null) {
return true;
}
console.log("Waiting for chat room JSON...");
return false;
}
// If there is a JSON request in progress, wait until it completes.
if (isReady()) {
onReady();
} else {
var delay = new goog.async.ConditionalDelay(isReady);
delay.onSuccess = onReady;
delay.onFailure = onFailure;
delay.start(500, 5000);
}
}
"while (json == null) { }" は同期的 (他のすべての JS 実行をブロックする) であるため、使用できないことに注意してください。