Friday, May 10, 2024
84
rated 0 times [  85] [ 1]  / answers: 1 / hits: 18230  / 13 Years ago, mon, september 26, 2011, 12:00:00

I have a class, ChatRoom, that can only render after it receives a long-running HTTP request (it could take 1 second or 30 seconds). So I need to delay rendering until ChatRoom.json is not null.



In the code below, I'm using Closure Library's goog.async.ConditionalDelay. It works, but is there a better way (maybe without needing Closure Library) to do this?



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);
}
}


Note that while (json == null) { } isn't possible because that would be synchronous (blocking all other JS execution).


More From » asynchronous

 Answers
8

Consider this:



(function wait() {
if ( chatroom.json ) {
chatroom.render();
} else {
setTimeout( wait, 500 );
}
})();


This will check every half second.



Live demo: http://jsfiddle.net/kBgTx/


[#89915] Sunday, September 25, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
haleeg

Total Points: 703
Total Questions: 100
Total Answers: 98

Location: Dominica
Member since Sat, Nov 5, 2022
2 Years ago
;