0

配列を実行して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);からの出力です。下。チェックボックスの値からいくつかのURLを取得します。

$('.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;
});

次に、これは、コールバックを実行しようとしている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');

          },
          error: function (response) {

                     callback('false');

          }
     });


}

これにより、phpファイルに送信されます。

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

私がやろうとしているのは、完全にダウンロードされた1つのURLからのコールバックの後で、配列から次の値を取得し、配列内のすべてのURLが完全にダウンロードされるまでそのURLをダウンロードするというように続きます。

現時点では、最初の値をダウンロードするだけで、trueの戻り値が返された後、ループを再開しないため、クラッシュします。

完了後にコールバックを使用して値の配列をループするための最良の方法について助けを探している人にとって、これが理にかなっていることを願っています。

4

2 に答える 2

2

この構造があなたを助けることができるかもしれません。このバリアントでは、前の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();
             // Addition logic if needed
          },
          error: function (response) {

          }
     });
    }

Run()
于 2012-09-23T13:53:47.877 に答える
0

もう少し時間ができたので、jqueryajaxが据え置きとして実装されているという事実を利用した代替案を示すのは良いことだと思いました。つまり、パイプチェーンを使用してすべての作業を行うことができます。また、延期された動作を利用してコールバックを排除しました。

これはあなたにアイデアを与えるはずです。

// Use jquery deferred pipe chaining to force 
// async functions to run sequentially


var dfd = $.Deferred(),
    dfdNext = dfd,
    x,
    values = [],

    // The important thing to understand here is that 
    // you are returning the value of $.ajax to the caller.
    // The caller will then get the promise from the deferred.
    ajaxRequest = function (urlSend) {

        var send = {
            url: urlSend
        }

        return  $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>index.php/upload",
            data: send,
        });
    };


// Starts things running.  You should be able to put this anywhere 
// in the script, including at the end and the code will work the same.

dfd.resolve();


// Deferred pipe chaining.  This is the main part of the logic.
// What you want to note here is that a new ajax call will 
// not start until the previous
// ajax call is completely finished.
// Also note that we've moved the code that would
// normally be in the callback.
// Finally notice how we are chaining the pipes by
// replacing dfdNext with the return value from the
// current pipe.
for (x = 1; x <= 4; x++) {

    values.push(x);

    dfdNext = dfdNext.pipe(function () {
        var value = values.shift();
        return requestAjax(value).
            done(function(response) {
                // Code here that you would have 
                // put in your callback.
                console.log(response);
            }).
            fail(function(response) {
                console.log(response);
            };

    });

}

jsFiddleで遊ぶことができる実例。

于 2012-09-26T20:38:21.447 に答える