刷新 javascript 代码

To refresh a javascript code

很好,我有一个 javascript 感谢这个网站,它来自一个 XML 文件,其 link 是:http://inlivefm.6te.net/NowOnAir.xml,javascript 必须删除功能作为艺术家姓名和歌曲 xml 文件。 我打算做的是添加 javascript 代码来自动刷新艺术家姓名和歌曲。

我试过了但是没有得到这个结果,下面我把javascript和"attempt"留到刷新。

<script type="text/javascript">
  $(document).ready(function(){
          /*  Busca el archivo NowOnAir.xml */
          $.ajax({
            type: "GET",
            url: "/NowOnAir.xml",
            dataType: "xml",
            success: function(xml) {
                /*Buscar el tag <Event> e ir repitiendo el proceso hasta el final (bucle) */
                $(xml).find('Event').each(function(){
                    /*Tomar los valores de startTime*/
                    var startTime = $(this).attr('startTime');

                    /*Buscar el tag <Song> y repetir bucle*/
                    $(this).find('Song').each(function(){
                        /*Tomar los valores de title*/
                        var title = $(this).attr('title');
                        var artist;

                        /*Buscar el tag <Artist> y repetir bucle*/
                        $(this).find('Artist').each(function(){
                            /*Tomar los valores de name*/
                            artist = $(this).attr('name');
                        });

                        /*Buscar el tag Expire y repetir bucle*/
                        $(this).find('Expire').each(function(){
                            var time = $(this).attr('Time');
                            $('.cancion').append("<p>"+artist+" - "+title+"</p>");
                            setInterval(function(){$('.cancion');}, 5000);
                        });
                    });
                });
            }
          });
        });
</script>
</head>
<body>
<div class="cancion"></div>
</body>
</html>

一定是我弄错了,所以才来求助。

既然已经非常感谢你了。

  1. 可能导致错误的主要原因是 closing braces & brackets 在一些地方丢失了。

  2. 使用setInterval功能自动刷新您的内容。

    <script>
    
    $(document).ready(function(){
    get_info();
    setInterval(function(){ get_info(); }, 20000);
    
    function get_info(){
    
            $.ajax({
                type: "GET",
                url: "NowOnAir.xml",
                dataType: "xml",
                success: function(xml) {                     
                      $(xml).find('Event').each(function(){                                  
                      $(this).find('Song').each(function(){                
                      var title = $(this).attr('title');
                      var artist;                
    
                      $(this).find('Artist').each(function(){                    
                        artist = $(this).attr('name');
                      });                
    
                      $(this).find('Expire').each(function(){                    
                      $('.cancion').append("<p>"+artist+" - "+title+"</p>");
                    });
                  });
                });
              }
           });
    }
    });
    
    </script>
    

如果您需要在每次 $.ajax 调用时替换 <div> 的内容,那么 使用 .html() 方法。

$('#content-wr').html('<div class="new-lines"><div><a>' + val.id + '&nbsp;&nbsp;&nbsp;' + val.joke + '</a></div></div>');

并检查这个例子:

setInterval(function(){    
  $.ajax ({
    url: 'http://api.icndb.com/jokes/random',
    method: 'GET',
    beforeSend: function(){
      $('#content-wr').children().fadeOut(1500);
    },
    success: function(data){
      var val = data.value
      $('#content-wr').append('<div class="new-lines"><div><a>' + val.id + '&nbsp;&nbsp;&nbsp;' + val.joke + '</a></div></div>');
    }
    });    
},5000);
.new-lines > div{
  width: 500px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="content-wr"></div>

您的 ajax 将每 5 秒被调用一次并内容 updated/refreshed。

或者将您的 ajax 调用放在 get_info() 函数中。