简体   繁体   English

每X秒自动运行一个PHP脚本,而无需刷新页面

[英]Automatically run a PHP script every X seconds without refreshing the page

I have a website, and in the navigation header, I have a player count, for the people who are currently online (On an external server). 我有一个网站,在导航标题中,有一个球员人数,这些球员是当前在线(在外部服务器上)的人员的。 The count is outputted as a raw number generated by PlayersOnline.php, and I just include that. 该计数作为PlayersOnline.php生成的原始数字输出,我只包括了它。

How would I have this page update the player count every X seconds without refreshing the page? 如何在不刷新页面的情况下让此页面每X秒更新一次播放器计数?

You can use Javascript and jQuery to solve this: 您可以使用Javascript和jQuery解决此问题:

$(function() {
    updateCounter();
});

function updateCounter() {
    $.ajax({
        url: 'yourScript.php',
        success: function(output) {
            $('#yourCounterElementID').text(output);
        },
        complete: function() {
            setTimeout(updateCounter(), 5000);//run again in 5 seconds
        }
    });
}

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

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