0

我需要在 ajax 请求后使用接收到的数据来淡入元素。它工作正常,但所有元素同时变得可见。是否可以在要显示的每个元素的可见性更改之间实现小的延迟?

  $.ajax({        
               type: "POST",
               url: "get_values.php",
               dataType: "json",
               data: { prm_listArray : prm_list},
               success: function(res) { 
                    $.each(res, function(key, value) {
                       var cell = $('table tr:eq(' + value.prm_row + ') td:eq(' + value.prm_column + ')');
                       $(cell).find(".cell_text").html(value.prm_value).fadeIn('slow');
                       })
                 }
            }); 

我试图插入延迟(3000)但没有成功:

$(cell).find(".cell_text").html(value.prm_value).delay(3000).fadeIn('slow');

并以相同的行为超时:

$(cell).find(".cell_text").html(value.prm_value);
                setTimeout(function() {
                    $(cell).find(".cell_text").fadeIn('slow');
                }, 3000);
4

1 回答 1

1

cell每次都在 for 循环中覆盖变量。所以它应该定义为范围变量。

$.each(res, function(key, value) {
  var cell = $('table tr:eq(' + value.prm_row + ') td:eq(' + value.prm_column + ')');
  (function(cell) {
    setTimeout(function() {
        $(cell).find(".cell_text").fadeIn('slow');
    }, 3000);
  })(cell);
});

添加的测试代码:

<!DOCTYPE HTML>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function() {
    var res = [
        { prm_row: 0, prm_column: 0 },
        { prm_row: 1, prm_column: 1 },
        { prm_row: 2, prm_column: 2 },
    ];
    $.each(res, function(key, value) {
        var cell = $('table tr:eq(' + value.prm_row + ') td:eq(' + value.prm_column + ')');
        (function(cell) {
            setTimeout(function() {
                $(cell).find(".cell_text").fadeIn('slow');
            }, 3000);
        })(cell);
    });
})
</script>
<style type="text/css">
#board .cell_text {
    display: none;
}
</style>
</head>
<body>
<table id="board">
    <tr>
        <td><span class="cell_text">x</span></td>
        <td><span class="cell_text">x</span></td>
        <td><span class="cell_text">x</span></td>
    </tr>
    <tr>
        <td><span class="cell_text">x</span></td>
        <td><span class="cell_text">x</span></td>
        <td><span class="cell_text">x</span></td>
    </tr>
    <tr>
        <td><span class="cell_text">x</span></td>
        <td><span class="cell_text">x</span></td>
        <td><span class="cell_text">x</span></td>
    </tr>
</table>    
</body>
</html>

一步步

<!DOCTYPE HTML>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function() {
    var res = [
        { prm_row: 0, prm_column: 0 },
        { prm_row: 1, prm_column: 1 },
        { prm_row: 2, prm_column: 2 },
    ];

    var f = function() {
        var value = res.shift();
        if (!value) return;
        var cell = $('table tr:eq(' + value.prm_row + ') td:eq(' + value.prm_column + ')');
        $(cell).find(".cell_text").fadeIn('slow');
        setTimeout(function() {
            f(res);
        }, 3000);
    };
    setTimeout(f, 3000);
})
</script>
<style type="text/css">
#board .cell_text {
    display: none;
}
</style>
</head>
<body>
<table id="board">
    <tr>
        <td><span class="cell_text">x</span></td>
        <td><span class="cell_text">x</span></td>
        <td><span class="cell_text">x</span></td>
    </tr>
    <tr>
        <td><span class="cell_text">x</span></td>
        <td><span class="cell_text">x</span></td>
        <td><span class="cell_text">x</span></td>
    </tr>
    <tr>
        <td><span class="cell_text">x</span></td>
        <td><span class="cell_text">x</span></td>
        <td><span class="cell_text">x</span></td>
    </tr>
</table>    
</body>
</html>

使用 setInterval:

    var timer = setInterval(function() {
        var value = res.shift();
        if (!value) {
            clearInterval(timer);
            return;
        }
        var cell = $('table tr:eq(' + value.prm_row + ') td:eq(' + value.prm_column + ')');
        $(cell).find(".cell_text").fadeIn('slow');
    }, 3000);
于 2012-11-29T10:59:03.540 回答