Ajax每5秒检查一次清除原始数据的xml文件

时间:2014-02-08 19:23:50

标签: jquery ajax xml

我有一些js在HTML上显示XML数据:

$(document).ready(function () {
      $.ajax({
        type: "GET",
        url: "xml/odds.xml",
        cache: false,
        dataType: "xml",
        success: function(xml) {
          var $match = $(xml).find('match[id="670358"]');
          var $result = $match.find('bet[code="Ftb_Mr3"]');
          var $odds = $result.find("choice");
          var game = $match.attr('name').replace('-','<span class="text-danger">vs</span>');

          $("#title").append(game);
          $odds.each(function () {
            odd = $(this).attr('odd');
            $("#odds").append('<li>' + odd + '</li>');
          });
    }
      });
  });

这很好但是当我尝试获取它时它每5秒检查一次XML文件,所以它显示为:

$(document).ready(function () {
    function get_info() {
      $.ajax({
        type: "GET",
        url: "xml/odds.xml",
        cache: false,
        dataType: "xml",
        success: function(xml) {
          var $match = $(xml).find('match[id="670358"]');
          var $result = $match.find('bet[code="Ftb_Mr3"]');
          var $odds = $result.find("choice");
          var game = $match.attr('name').replace('-','<span class="text-danger">vs</span>');

          $("#title").append(game);
          $odds.each(function () {
            odd = $(this).attr('odd');
            $("#odds").append('<li>' + odd + '</li>');
          });
    }
      });
    }
    setInterval(function() {
      get_info();
    }, 5000);
  });

这虽然会延迟开始,然后重复数据。

如何编辑它以便它立即显示XML中的数据,然后每次检查时清除数据,这样我就可以显示一组数据。

任何帮助都是适当的,之后只需要弄清楚如何将小数改成分数

1 个答案:

答案 0 :(得分:0)

啊清楚是如此简单已经清除错误的ID我添加:

$('#title, #odds').html(''); //Clear content

现在看起来像:

$(document).ready(function () {
    function get_info() {
      $.ajax({
        type: "GET",
        url: "xml/odds.xml",
        cache: false,
        dataType: "xml",
        success: function(xml) {
          $('#title, #odds').html(''); //Clear content
          var $match = $(xml).find('match[id="670358"]');
          var $result = $match.find('bet[code="Ftb_Mr3"]');
          var $odds = $result.find("choice");
          var game = $match.attr('name').replace('-','<span class="text-danger">vs</span>');

          $("#title").append(game);
          $odds.each(function () {
            odd = $(this).attr('odd');
            $("#odds").append('<li><a class="btn btn-danger" href="#">' + odd + '</a></li>');
          });

    }
      });
    }
    setInterval(function() {
      get_info();
    }, 5000);
  });

现在首次加载时只需解决延迟:)