循环通过数组与回调 [英] Looping through array with callback

查看:147
本文介绍了循环通过数组与回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行一个数组发送到一个php文件和回调发送下一个值,php完成其下载后。这里我到目前为止。



我的数组通过如下。

  [http://example.com/test1.zip,http://example.com/test2.zip,http://example.com/test3.zip,http:/ /example.com/test4.zip,http://example.com/test5.zip] 

是console.log(values)的输出;下面。它会从复选框值中获取一些网址。

  $('。geturls')。live('click',function 

var values = new Array();
$ .each($(input [name ='downloadQue []']:checked),function(){
values.push($(this).val());

ajaxRequest($(this).val(),function(response){

console.log响应);

});

});

console.log(values);

return false ;
});

然后调用一个ajax函数,我试图做一个回调。

  function ajaxRequest(urlSend,callback){

var send = {
url:urlSend
}

$ .ajax({
type:POST,
url:<?php echo base_url();?> index.php / upload
data:send,
// dataType:json,
// timeout:8000,
beforeSend:function(){

}
success:function(response){

callback('added');

},
错误:function(response){

callback('false');

}
});


}

然后会发送到php文件

  function upload(){
$ output = shell_exec(wget {$ _POST ['url']} 2& 1);
return true;
}

我要做的是从一个url下载完全然后从数组中获取下一个值,并下载该网址,依此类推,直到数组中的所有网址完全下载。



第一个值,然后崩溃,因为它不会重新启动循环,返回true的返回值。



希望这是有意义的人只是寻找一些帮助

解决方案

可能是这种结构可以帮助你。在此变体中,仅在成功完成先前的Ajax调用后才转到下一个URL。

  var arr = ['url0','url1','url2','url3']; 
var index = 0;

function Run(){
DoAjax(arr [index]);
}
function next(){
if(arr.count = index-1)
{
index = 0;
return;
} else {
DoAjax(arr [index]);
}
}

function DoAjax(url){

$ .ajax({
type:POST,
url:url,
data:send,
beforeSend:function(){

},
success:function(response){
index + +;
Next();
//需要时添加逻辑
},
错误:function(response){

}
});
}

Run()


I am trying to run through a array send to a php file and on a callback send the next value after the php has completed its download. Here what i have so far.

my array come through as follows.

["http://example.com/test1.zip", "http://example.com/test2.zip", "http://example.com/test3.zip", "http://example.com/test4.zip", "http://example.com/test5.zip"] 

above is the output from console.log(values); below. it grabs some urls from checkbox values.

$('.geturls').live('click',function(){

    var values = new Array();
    $.each($("input[name='downloadQue[]']:checked"), function() {
      values.push($(this).val());

       ajaxRequest($(this).val(),function(response){

            console.log(response);

       });  

    });

    console.log(values);

    return false;
});

this then calls a ajax function which i am trying to do a callback on.

function ajaxRequest(urlSend,callback){

    var send = {
            url: urlSend
        }

    $.ajax({
          type: "POST",
          url: "<?php echo base_url(); ?>index.php/upload",
          data: send,
          //dataType: "json",
          //timeout: 8000,
          beforeSend: function() {

          },
          success: function(response) {

             callback('added');

          },
          error: function (response) {

                     callback('false');

          }
     });


}

this will then send to a php file.

function upload(){
   $output = shell_exec("wget {$_POST['url']} 2>&1");      
   return true;
}

What i am trying to do is after the callback from one url which it has download fully then grab the next value from the array and download that url and so on until all the urls in the array are downloaded fully.

at the moment it just downloads the first value and then crashes because it doesn't restart the loop after a return value of true is returned.

Hope this makes sense to someone just looking for some help on the best way to loop through an array of values with a callback after complete.

解决方案

May be this structure can help you. In this variant you go next URL only after successful completion of the previous Ajax call.

    var arr = ['url0','url1','url2','url3'];
    var index = 0;

    function Run(){
         DoAjax(arr[index]);
    }
    function Next( ){
        if(arr.count = index-1)
        {
             index =0;
             return;  
        }else{
           DoAjax(arr[index ]);
        }
    }    

    function DoAjax(url){

         $.ajax({
          type: "POST",
          url: url,
          data: send,
          beforeSend: function() {

          },
          success: function(response) {
             index ++;
             Next();
             // Addition logic if needed
          },
          error: function (response) {

          }
     });
    }

Run()

这篇关于循环通过数组与回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆