Building an AJAX-Based Chat: The Barebones Structure - Checking for the progress of sender requests: defining the “senderStatusChecker()” function
(Page 4 of 5 )
Once a user has submitted a message, the application needs to determine whether the post request has been successfully completed. If it has, the display of messages will be immediately updated, which translates into a faster visual response for the user that originally sent the message. Therefore, considering that updating the messages currently displayed can be easily handled after the insertion of a new message in the database table, here is the definition for the “senderStatusChecker()” function:
function senderStatusChecker(){
// check if request is completed
if(senderXMLHttpObj.readyState==4){
if(senderXMLHttpObj.status==200){
// if status == 200 display chat data
displayChatData(senderXMLHttpObj);
}
else{
alert('Failed to get response :'+
senderXMLHttpObj.statusText);
}
}
}
As you can see, this function is a lot simpler than it really seems. What it does essentially is check the status of the post request responsible for inserting a new message into the database. In that case, the display of messages needs to be quickly updated. If you study the above code, you’ll see that the updating process is performed by the “displayChatData()” function, which accepts a requester object as the unique incoming parameter.
Having illustrated how the progress of the requests made by the “sender” object is checked in, let’s now take a look at the “displayChatData()” function, and see how the display of chat messages is properly updated. Just keep reading.
Next: Updating the display of messages: coding the “displayChatData()” function >>
More XML Articles
More By Alejandro Gervasio
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|