views:

36

answers:

2
   for (var i=0;i<ToArray.length;i++) {
    $.post('jquery/messages.php',{To:ToArray[i],subject:subject,msg:msg},function(data){jAlert(data);}); 
  } 

User can send short messages to another user(s). The names are commaseparetad and after i split the string i want to send the same message to the different users with a for loop.

if something goes wrong (eg. there is no such user in the database)the server alerts the user and stop the process. If the user have pressed the "ok" button the process continues.

my problem is that the loop never stops and it alerts only the last wrong name.

A: 

This may be because the default mode for $.post is to run asynchronous, and therefore the loop runs through to the end, the posts are queued by the browser. When an error finally occurs, the loop has ended and the last record is in place.

Try changing to use $.ajax with async set to false.

Clicktricity
ok it helped but only with normal alert. with jAlerts has the s
Mstrfgsk
...same problem
Mstrfgsk
A: 

Just as a first pass, this in highly inefficient. Wouldn't it be better to pack the response object with the message, the subject, and the list of users, and have the server do the unpacking? It would reduce bandwidth and latency on the client side from O(n) to O(1)! Something like:

$.post('jquery/messages.php',{To:ToArray,subject:subject,msg:msg},function(data){jAlert(data);});

This would give your server the capability to try and send to every user, and send back a message for all users for whom the message failed, rather than just the first.

Elf Sternberg