M
marty3d
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', (e-mail address removed)');
aList[1] = new Array('2', '(e-mail address removed)');
aList[2] = new Array('3', '(e-mail address removed)');
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
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', (e-mail address removed)');
aList[1] = new Array('2', '(e-mail address removed)');
aList[2] = new Array('3', '(e-mail address removed)');
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