Ajax Mess试图创建SetInterval函数



好吧,我之前问了一个问题,但我遇到了一个全新的问题。我在 PHP 脚本服务器端有一个 PHP 数组。我正在尝试编写一个客户端 Ajax 脚本来重新轮询 PHP 脚本以获取新数据并更新页面上显示的统计信息。

这是我确定我做得不对的 Ajax:

setInterval(function(getLatestInfo) 
{  
    $.ajax({
    type: 'POST',
    url: 'script.php',
    data: 'id=data',
    dataType: 'json',
    cache: false,
    success: function(getLatestInfo){
    $.getJSON(script.php, function(getLatestInfo){
    var result_array = JSON.parse(result);
    });
    $('#bit_rate').html(result_array[4]);
    $('#listeners').html(result_array[5]);
    $('#current_song').html(result_array[9]);
                });
            });
            }, 10000);//time in milliseconds  

这是PHP:

            <?php 
            Function getLatestInfo() {
$SERVER = 'http://chillstep.info:1984'; 
$STATS_FILE = '/status.xsl?mount=/test.mp3'; 
$LASTFM_API= '00000000000000000'; 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL,$SERVER.$STATS_FILE); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
$output = curl_exec($ch); 
curl_close($ch); 
$dp = array(); 
$search_for = "<tds[^>]*class="streamdata">(.*)</td>"; 
$search_td = array('<td class="streamdata">','</td>'); 
if(preg_match_all("/$search_for/siU",$output,$matches)) { 
   foreach($matches[0] as $match) { 
      $to_push = str_replace($search_td,'',$match); 
      $to_push = trim($to_push); 
      array_push($dp,$to_push); 
   } 
} 
$x = explode(" - ",$dp[9]); 
echo json_encode($dp);
            }
 ?>

简而言之,我需要这个 ajax 脚本来更新和拉取 PHP 变量$dp,这是一个数组,解析它,并创建 HTML 可用的字符串变量。并每10秒重复该过程。

不要在此实例中使用 setInterval。在成功回调中再次调用原始函数。此外,您在 ajax 调用的成功回调中调用 getJSON - 这是重复调用。

例如

函数 getlatest(){  $.ajax({  阿贾克斯参数的其余部分  成功:函数(结果){    var resultarray = $.parseJSON(results);    $('#bit_rate').html(result_array[4]);    $('#listeners').html(result_array[5]);    $('#current_song').html(result_array[9]);    getlatest();  }  });}

最新更新