Don't understand flow using xmlhttp



Hi!
I'm building a simple newsletter application using Classic ASP. In
order to avoid page timeout, I thought of using AJAX to send each mail
asynchronously.

So, my thought was
1. create an array using an ordinary asp recordset parsed to js.
2. loop through the array and for each e-mail address do a http
request for a ASP page doing the validation and sending the mail to
the current email address

This worked out ok, the mails go out, but not in the order they are
sent to the for-loop. A more serious problem appear when I try to add
a emails-to-go-counter and some "what's-currently-happening" in the
loop.

Here's my real problem exemplified:
In the below example, I set the innerHTML of resultdiv to 'Wait...' .
When the loop through mail addresses is done, it should be changed to
'Done!'. But what's really happening is that the innerHTML is changed
way sooner so when it says 'Done!', the mails are still processed.
Is there a way of not continuing the loop until the response has
arrived or something? After several years of programming, this
behaviour is new to me :(

Here's a simplified code for the process:
<script>
function myFunction(){
var aList = new Array();
var resultdiv = document.getElementById('result');
resultdiv.innerHTML='Wait...';

aList[0] = new Array('1', mail1@xxxxxxxx');
aList[1] = new Array('2', 'mail2@xxxxxxxx');
aList[2] = new Array('3', 'mail3@xxxxxxxx');

var iNumberOfItems = aList.length;
for(var x=0;x<a0_len;x++){
makeHttpRequest('send_ajax.inc.asp?ajax_send='+aList[x]
[1]+'&issue='+<%=issueid%>, 'fAlert');
}
resultdiv.innerHTML='Done!';
}

//This is tastelessly copied from an example found on the net... I
will make my own as soon as I understand the above problem :)
function makeHttpRequest(url, callback_function, return_xml){
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!http_request) {
alert('Unfortunatelly you browser doesn\'t support this feature.');
return false;
}
http_request.onreadystatechange = function() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
if (return_xml) {
eval(callback_function + '(http_request.responseXML)');
} else {
eval(callback_function + '(http_request.responseText)');
}
} else {
alert('There was a problem with the request.(Code: ' +
http_request.status + ')');
}
}
}
http_request.open('GET', url, true);
http_request.send(null);
}
function fAlert(text){
//Do something with the response
}
</script>

Thanks alot!
/Martin
.



Relevant Pages

  • Problem with ASP/Javascript coding with frames and "top.parent"
    ... I am fixing a client's Web site and I do not have the time to reprogram ... When a different value is selected in a drop-down, a piece of ASP is ... var f = top.parent.main.document.frmPayment; ...
    (comp.lang.javascript)
  • Re: ASP Loop in wrong place????
    ... below is the code for the entire table (inc. ASP functions) ... face="Verdana, Arial, Helvetica, sans-serif"> ... >>the column data and the 3rd is where the loop for the SQL statement lies. ...
    (microsoft.public.inetserver.asp.general)
  • Re: slowing a script down
    ... When I add an alert to check it for each step of the loop, ... var stringWidth = new String; ... panelwidth = the character position where the string px is first found? ... can be re-used to animate any panel. ...
    (microsoft.public.scripting.jscript)
  • Re: Illegal LOOP usage?
    ... | reason would they even mention the WITH clause there? ... On the next iteration the loop variable would be bound with WITH to ... the constructs collect and collecting collect ... The argument var is set to the ...
    (comp.lang.lisp)
  • Re: ASP Loop in wrong place????
    ... Thanks however as i will build my ASP apps dynamically in the future. ... >>about - note the extra row for the end of loop. ... > Helvetica, sans-serif";} ... > ' Get your record set before beginning to create the table ...
    (microsoft.public.inetserver.asp.general)