簡體   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