2013-08-04 3 views
0

라디오 웹 사이트에서 자바 스크립트 노래 카운트 다운을 사용합니다. 매 10 초에서아약스 호출로 인해 자바 스크립트 카운트가 지연됩니다.

, 카운트 다운 시차는 새 노래가 있는지 확인하기 때문에 아약스 호출의 (1 하나 더 초 건너 뛰기) :

function getcursong(){ 
    var result = null; 
    var scriptUrl = "get_current_song.php"; 
    $.ajax({ 
    url: scriptUrl, 
    type: 'get', 
    dataType: 'html', 
    async: false, 
    success: function(data) { 
     result = data; 
    } 
    }); 
    return result; 
} 

콘솔에서 보면, 나는 GET 볼 통화에는 약 2 초가 걸립니다.

get_current_song.phpcurl을 사용하여 icecast 서버에서 현재 노래를 가져옵니다.

아약스 호출로 인해 카운트 다운이 지연되는 것을 막기 위해 할 수있는 일이 있습니까?

이하는 get_current_song.php입니다.

<?php 
require('config.php'); 
$current_song = getStreamInfo(); 
function url_get_contents($Url) { 
    if (!function_exists('curl_init')){ 
     die('CURL is not installed!'); 
    } 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $Url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch,CURLOPT_TIMEOUT,2); 
    $output = curl_exec($ch); 
    curl_close($ch); 
    return $output; 
} 
function getStreamInfo(){ 
    $str = url_get_contents(SERVER.'/status.xsl?mount='.MOUNT); 
    if(preg_match_all('/<td\s[^>]*class=\"streamdata\">(.*)<\/td>/isU', $str, $match)){ 
     $x = explode(" * ",$match[1][9]); 
     return $x[1]; 
    } 
} 
?> 
<?php 
    echo $current_song; 
?> 
+1

@ ASYNCHRONOUS 통화 동기화를하지 마십시오.) – Andreas

+0

@Jeffman이 맞습니다. 필요하지 않다면'async : false' (기본은 암시 적'async : true')를 제거하십시오. – ale

+0

고마워요! 그것은 그 것이었다. – trogne

답변

4

나는 async: false이 카운트 다운 기능을 가로 채고 있다고 가정합니다. 거짓 일 필요가 있습니까?

+0

와우! 내가 참조. 더 이상 지체가 없습니다. 빠른 답장을 보내 주셔서 감사합니다. – trogne

+0

async가 true이므로 작동하지 않습니다. 위 참조 – trogne

0
function() 
{ 
var result = null; 
var scriptUrl = "get_current_song.php"; 
$.ajax({ 
    url: scriptUrl, 
    type: 'get', 
    dataType: 'html', 
    async: true, // NEEDED TO BE TRUE 
    success: function(data) {  // on success, I get the data 
     song = $('#song').html(); 
     if (data != song && data != ""){ 
      setTimeout(function(){ $('#nowplaying').load('nowplaying.php'); }, 5000); // pourquoi fadeIn("slow"); ?!? 
     }    
    } 
}); 
}, 10000); 
관련 문제