更改for循环以一次执行一行的函数

时间:2012-03-20 15:09:44

标签: jquery loops jsonp datatables

我有一个函数,它将遍历表的每一行,从该单元格中获取数字,将该数字发送到url以获取json响应,然后根据该行打印一些内容。我几乎让它工作但是现在它只是将这些全部抛弃,我希望它使用类似.each的东西来遍历每一行,做它的东西然后移动到下一行。我也在使用jquery数据表。脚本是这样的:

$j('#imageCheck').click(function(){
    var cells = [];
    var rows = oTable.fnGetNodes();
    for( var i=0;i<rows.length;i++){
        grabsku = $j(rows[i]).find('td:eq(2)').text();
        imgreplace = $j(rows[i]).find('td:eq(2)');
        s7url = 'http://checkit.com/is/image/' + grabsku;       

        $j.ajax({
            type: 'GET',
            url: s7url,
            data: 'req=exists,json',
            dataType: 'jsonp',
            beforeSend:function(){
                imgreplace.html('checking ..');
            },
            success: function(){
                imgreplace.html(z);
            }
        });
    }
});

响应和一切正常,我的问题是如何逐个循环这些。因此,在此示例中,imgreplace.html('checking ..');同时发生在表中的每一行。我希望它只处理那一行,然后success继续前进到下一行。

更新

为了更好地解释我为什么这样做,我同意这是不自然的,我从每个单元格抓取的数据有助于形成一个独特的网址,s7url。其中每个都从我无法控制的服务器返回这样的响应:

s7jsonResponse(
{"catalogRecord.exists":"0"},"");

然后我做了一些事情,知道这样的真假:

function jsonResponse(response){    
    x = response["record.exists"];  
    z = x == "0" ? "NO" : "YES";
}

我喜欢scrappedcola的解决方案,但它让我发现成功永远不会被解雇。我不知道为什么会这样。我可以在检查器选项卡中看到有一个像我上面粘贴的响应。我试图将成功转移到自己的功能中,例如:

success: function(){ success(); } 

...

var success = function(){
    imgreplace.html(z);
    i++;
    handleImageCheck(i);    
}

但这没有帮助..

更新2

所以我放弃了尝试让变量成功。相反,我将与你分享我非常丑陋的黑客。如果错误响应成功,ick。

error: function(data, status){
        if (status = "parseerror") {
            imgreplace.html(z);
            i++;
            handleImageCheck(i);    
        }
    }

更新3

如果由于某种原因有人关心,我找到了success未解雇的另一个问题的解决方案。我需要将jsonpCallback添加到选项中,然后将响应作为succcess中的函数进行处理。

2 个答案:

答案 0 :(得分:0)

类似的东西可能对你有用。基本上你应该对进行异步调用的图像检查功能进行1次调用。然后,在异步调用成功后转到下一行。

var i;  
  $j('#imageCheck').click(function(){ 
     i = 0;
     handleImageCheck(i);
  })
  var handleImageCheck = function(i){
     var cells = [];
     var rows = oTable.fnGetNodes();
     if($j(rows[i]).length)
     {     
    grabsku = $j(rows[i]).find('td:eq(2)').text();
    imgreplace = $j(rows[i]).find('td:eq(2)');
    s7url = 'http://checkit.com/is/image/' + grabsku;       

    $j.ajax({
      type: 'GET',
       url: s7url,
       data: 'req=exists,json',
       dataType: 'jsonp',
       beforeSend:function(){
          imgreplace.html('checking ..');
       },
       success: function(){
          imgreplace.html(z);
          i++;
          handleImageCheck(i);
       }
     });
    }
  }

答案 1 :(得分:0)

如果你想要,你应该使用一点递归

function makeCall(rows, index) {
    //if index is undefined, it's the first call, let's start from the first element
    if (index === undefined) {
        index = 0;
    }
    row = rows[index];
    grabsku = $j(row).find('td:eq(2)').text();
    imgreplace = $j(row).find('td:eq(2)');
    s7url = 'http://checkit.com/is/image/' + grabsku;

    $j.ajax({
        type: 'GET',
        url: s7url,
        data: 'req=exists,json',
        dataType: 'jsonp',
        beforeSend: function() {
            imgreplace.html('checking ..');
            var nextIndex = index + 1;
            //if there is anothe element in rows, make another call)
            if (rows[nextIndex] !== undefined) {
                makeCall(rows, nextIndex);
            }
        },
        success: function() {
            imgreplace.html(z);
        }
    });
}

       var rows = oTable.fnGetNodes();
       makeCall(rows);
BTW - AJAX很酷,因为你可以一次拨打很多电话,等待这样对我来说似乎不自然!