JQuery Condition [仅在不同的情况下更改数据]

时间:2013-05-20 13:54:49

标签: jquery

我有这个Jquery代码,我想只在与新数据不同时刷新内容 $title.html的html代码为"div#title>h4"

function onair() {
    $.ajax({
        url: 'player.php', // my json content file
        data: "",
        dataType: 'json',
        success: function (data) {
            var artiste = data[0];
            var titre = data[1];
            var title = data[2];
            var cover = data[3];
            var $cover = $("#cover"),
                $title = $("#title");

            if ( $title.html != title ) {
                $cover.fadeOut(300, function (data) {
                    $cover.html("<img src='" + cover + "' width='220px' height='210px'>");
                    $cover.fadeIn(300);
                });
                $title.fadeOut(300, function (data) {
                    $title.html("<h4> " + title + " </h4>");
                    $title.fadeIn(300);
                });
            }

            setTimeout("onair()", 1000);

        }

感谢您的帮助:)

2 个答案:

答案 0 :(得分:1)

进行以下两项修改:

1

// note that `text` must be invoke
if ( $title.text().trim() != title  ) 

2

setTimeout(function () {
    onair();
}, 1000);

答案 1 :(得分:0)

您应该在ajax请求中添加“last-time”额外参数。在歌曲(服务器端)上必须有“上次修改时间”,并且比较日期,如果歌曲日期高于请求日期,则发送它。

不要忘记在您的ajax响应中发布“最后一次”参数,以存储在您的客户端并在下次请求中发送。