gpt4 book ai didi

php - 由于ajax调用,javascript倒计时滞后

转载 作者:行者123 更新时间:2023-11-28 15:57:46 25 4
gpt4 key购买 nike

我在我的广播网站上使用了 javascript 歌曲倒计时。

每 10 秒,倒计时就会滞后一次(再跳过 1 秒),因为 ajax 调用会检查是否有新歌曲:

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.php 使用 curl 从icecast服务器获取当前歌曲。

我可以采取什么措施来防止倒计时因这些 ajax 调用而滞后?

下面是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;
?>

最佳答案

我假设 async: false 正在劫持您的倒计时功能。你需要它是假的吗?

关于php - 由于ajax调用,javascript倒计时滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18046776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com