开发者

jquery repeat process once get reply from php file

i have one php file which process adding of record in Database fro array.

for example in array i have 5 items

        aray an='abc','xyz','ert','wer','oiu'

i want to call one php file in j query ajax method

   um = an.split(',');
    var counter = 0;
   if(counter < uemail.length) {
   $("#sending_count").html("Processing Record "+ ecounter +" of " + an.length);
   var data = {uid: um[counter]
   $.ajax({
   type: "POST",
  url: "save.php",
  data: data,
   success: function(html){
   echo "added";
   counter++; 
   }

what it do, it complete all the prcess but save.php is still working what i want after one process it stop untill process of save.php complete then i开发者_JAVA百科t wait for next 10 sec and start adding of 2nd element.

Thanks


Not sure if I understand your issue correctly, but you may want to use synchronous (blocking) ajax calls instead of asynchronous (non-blocking). When you make asynchronous call, code execution continues immediately, leaving ajax calls "in the background". Synchronous call blocks the code execution until the request has finished.

$.ajax({ async: false, ... });


It not my place to question why you would want to do this, although want you are trying could result in a slow and unresponsive UI.

You'll want a while loop, not an if loop:

while(counter < uemail.length) { 

Other solution that present themselves,

You'll want to turn off the async flag to ensure the call is complete, before executing the next line. The delay() function will also help.

$.ajax({   
    async: false,  //ensure our requests are synchronous.
    type: "POST",   
    url: "save.php",   
    data: data,   
    success: function(html){   
        echo "added";  //?? not a valid javascript function
        delay(10000);  //10000ms = 10 seconds.
    }
    counter++;    

}

Also,echo is not a valid jQuery/javascript function, and your braces are somewhat unclear, and probably misssing.

I have assumed above that counter++ is outside your loop, because if it wasn't and you got a failure then it could continue forever.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜