You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

替代AJAX结合setTimeout实现视频数据定时刷新的方案有哪些?

Displaying Your Top-Ranked Video via AJAX

Here's a polished implementation of your script that fetches video data from your GoDaddy virtual host and dynamically displays the top-ranked video on your PHP page. The core logic uses jQuery's AJAX to pull JSON data and update the page content:

<script id="source" language="javascript" type="text/javascript">
$(function refreshscreen() {
    $.ajax({
        url: 'screen.php', // This endpoint returns your top video data as JSON
        data: "",
        dataType: 'json',
        success: function(data) {
            // Extract key details from the JSON response
            var id = data[0];
            var name = data[1];
            var votes = data[2];
            var videoUrl = data[3];
            var thumbnailImage = data[4];

            // Update your page elements with the top video content
            // Replace these selectors with your actual HTML element IDs/classes
            $('#video-title').text(name);
            $('#vote-count').text(`Total Votes: ${votes}`);
            $('#main-video-player').attr('src', videoUrl);
            $('#video-thumbnail').attr('src', thumbnailImage);
        }
    });
});

// Optional: Auto-refresh to show the latest top video every 30 seconds
setInterval(refreshscreen, 30000);
</script>

Important Details to Note:

  • Backend Setup: Ensure screen.php correctly queries your database (or data source) to retrieve the video with the highest votes, then outputs a JSON array in the order: [id, name, votes, video_url, thumbnail_url].
  • DOM Integration: Replace the example selectors (#video-title, #main-video-player, etc.) with the actual elements in your PHP page where you want to display the video and its metadata.
  • Error Handling: For robustness, add an error callback to the AJAX request to handle cases where the data fetch fails (e.g., network issues or server errors).

内容的提问来源于stack exchange,提问作者Dustin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:53:33