0

jquery の .each メソッドは、ajax 呼び出しが成功するまで待機しません。

$("img[name='statusIcon']").each(function () {
    var statusIcon = $(this);
    statusIcon.attr('src', 'images/spinner.gif'); //loading image will be here
    var row = $(this).parent().parent().attr("id");
    var HostName = $("#" + row + " td[name='Public']").attr("title");
    var Pass = $.trim($("#pass_word" + row).attr("realValue"));
    $.ajax({
        type: "POST",
        url: "validate.php",
        data: "HostName=" + HostName + "&uName=root&Pass=" + Pass + "&localCounter=" + stepCounter,
        success: function (res) {
            console.log(res);
            if (res == 0) {
                statusIcon.attr('src', 'images/tick-mark.jpg'); //success image will be here
            } else {
                statusIcon.attr('src', 'images/cross_icon.jpg'); //fail image will be here
            }
        }
    });
});

ここにコードがありますFIDDLE !

.each は、最初の ajax 呼び出しが応答するまで完了しますが、ajax 呼び出しごとにループする必要があります。
前の ajax 呼び出しが終了すると、ループを続行します。

4

1 に答える 1

0

async:false を使用しない試行

function doSomething(array){
    if(array.length == 0){
        return;
    }
    var statusIcon = $(array.shift());
    statusIcon.attr('src', 'images/spinner.gif'); //loading image will be here
    var row = $(this).parent().parent().attr("id");
    var HostName = $("#" + row + " td[name='Public']").attr("title");
    var Pass = $.trim($("#pass_word" + row).attr("realValue"));
    $.ajax({
        type: "POST",
        url: "validate.php",
        data: "HostName=" + HostName + "&uName=root&Pass=" + Pass + "&localCounter=" + stepCounter,
        success: function (res) {
            if (res == 0) {
                statusIcon.attr('src', 'images/tick-mark.jpg'); //success image will be here
            } else {
                statusIcon.attr('src', 'images/cross_icon.jpg'); //fail image will be here
            }
        }
    }).always(function(){
        doSomething(array)
    });
}
doSomething($("img[name='statusIcon']").get())
于 2013-10-11T07:16:12.377 に答える