Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-17-2012, 10:28 AM   PM User | #1
penser
New to the CF scene

 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
penser is an unknown quantity at this point
Invoking servlet with xhttp and status messages

I have an issue to save a long base64 String to a database/file.
I would like to show some message/status in a text field like 'Writting...' before and than use download function (which calls a servlet) like this:

function doDownload(pageId) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
displayStatus('Ready');
};
xhttp.open("GET", "http://127.0.0.1:7101/testApp/images?pageId=" + pageId, false);
xhttp.send();
}


However, when I call a function to display status 'Writting' at the beginning:

function displayStatus(status) {
var field = AdfPage.PAGE.findComponentByAbsoluteId('itStatus');
if (field) {
field.setValue(status);
}
}


It is not displaying the message 'Writting' (probably because it all happens in a one thread.

Could you forum users tell me how to show a status message before really invoking servlet action?

kind regards,
Christopher
penser is offline   Reply With Quote
Old 12-17-2012, 01:30 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Usually it helps to give the browser "some time" to perform the action before you start the request, even more so if you are starting a synchronous request which will freeze the browser.

Code:
displayStatus("Writing...");
window.setTimeout(function() {doDownload(whatever); }, 300);
devnull69 is offline   Reply With Quote
Old 12-17-2012, 09:23 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
If you would change to an ASYNCHRONOUS call, you'd solve THREE problems at once.

(1) Your "Waiting.." would display as soon as send() is called.
(2) You would not be freezing JavaScript while waiting for the send to complete.
(3) You could actually check to see if the operation completed normally.

Code:
function doDownload(pageId) 
{
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function ()
    { 
        if (xmlhttp.readyState==4 )
        {
            displayStatus (  ( xmlhttp.status==200) ? "Ready" : "Error " + xmlhttp.status );
        }
    }
    xhttp.open("GET", "http://127.0.0.1:7101/testApp/images?pageId=" + pageId, true);
    xhttp.send();
    displayStatus('Waiting'); // this line could actually be first in this function or before send...makes no difference

}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
xhttp status

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:43 AM.


Advertisement
Log in to turn off these ads.