繁体   English   中英

自动刷新<div>

[英]Automatically Refreshing <div>

我正在尝试创建一段代码,每5秒自动刷新一次。 它不起作用,我不知道为什么。 我一直在找几个小时。

JavaScript的:

$(document).ready(function(){
    refreshTable();
});

function refreshTable(){
    $('#songname').load('<?php include_once 'http://api.flare-radio.com/php/song/' ;?>', function(){
        setInterval(function(){

        }, 5000);

    });
}

HTML代码:

<div class="container1">
    <div class="song container">
        <div id="songname"></div>
        <br/>
        <h2 class="song">Presenter: <?php include_once 'http://api.flare-radio.com/php/presenter/' ;?></h2>
        <br/>
        <br/>
        <audio id="radioplayer" src="http://srv.flare-radio.com:8000/stream" autoplay></audio><input id="volume" name="volume" min="0" max="1" step="0.01" type="range" onchange="setVolume()"/>
    </div>
</div>

您希望每5秒执行一次的部分必须位于作为setInterval参数传递的函数中。

使用Javascript:

$(document).ready(function(){
    refreshTable();
});

function refreshTable(){
    setInterval(function() {
        $.get('stream_path.php', function(data)  { //maybe adjust path to php file
            $("#songname").html(data);
        }, 'text');
    }, 5000);
 }

编辑:上方和下方

检查上面的代码段进行调整。 我建议使用jQuery的get函数,并从其他php文件返回数据(由于js位于客户端,因此无法直接从js工作。php在服务器上运行)

所以我的最终解决方案(检查并为我工作)

使用Javascript:

-往上看

stream_path.php:

$url = 'http://api.flare-radio.com/php/song/';
echo file_get_contents($url);

html内容:

<div class="container1">
    <div class="song container">
        <div id="songname"><?php echo file_get_contents('http://api.flare-radio.com/php/song/'); ?></div>
        <br>
        <h2 class="song">Presenter: <?php echo file_get_contents('http://api.flare-radio.com/php/presenter/') ;?></h2>
        <br>
        <br>
        <audio id="radioplayer" src="http://srv.flare-radio.com:8000/stream" autoplay></audio><input id="volume" name="volume" min="0" max="1" step="0.01" type="range" onchange="setVolume()"/>
    </div>
</div>
    setInterval(function(){
   $('#songname').load('<?php include_once 'http://api.flare-radio.com/php/song/' ;?>' 
 }, 5000);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM