从 SQL 数据库自动更新统计信息


Auto Update Stats From SQL Database

我正在尝试获得像 http://www.minecraft.net 页面那样的效果,它从数据库中自动更新销售,我已经研究了两个月了,但没有运气。我有一个php文件,它可以查找数据库中有多少结果并将它们显示为数字,工作正常 http://tzeale.com/sandbox/stats/pull.php

我正在尝试做的是获得像 minecraft.net 一样的效果,它会自动更新而无需刷新页面。谁能指导我该怎么做?我不知道还能尝试什么。

谢谢。

h你需要使用 AJAX。

setTimeout,以及对该拉取的 AJAX 调用.php

如果你正在使用jQuery,这里有一个关于如何实现你想要的很好的例子。添加了一个简单的逻辑来查看服务器是否死机,并最终停止。

var failed = 0; 
var limit_failed = 5;
(function updateSales( waitTime ){
   waitTime = waitTime || 1000; // Set to 1 second by default
   setTimeout(function(){
       $.ajax({
           url: 'pull.php',
           success: function( response ){
               // Update something with your response
                alert ("sales are now at: "+ response);
               updateSales(); // Recursion
           },
           error: function(){           
                // Error handling - not necessary
               // If the request failed more then (limit_failed) times, you might want to stop it from checking after (limit_failed) times, 
               // and in the meanwhile icnrease the wait time, so the server has more time to get back online.
               if( ++failed < limit_failed ){
                   waitTime += 1000;
                   updateSales( waitTime );
               }
           }
       });
   }, waitTime);
})();

你会使用setTimeout和Ajax。 setTimeout 将使用 Ajax 每 1000 毫秒(或您如何设置)获取数据。

例如,您可以将显示计数包装在html中,如下所示:

<span id="mycount"></span>

那么你的jQuery代码将看起来像这样:

 setTimeout(function(){
   $.get("/sandbox/stats/pull.php",function(data){
      $("#mycount").html(data);
   });
 },1000);

1000是一秒钟,您可以根据需要进行更改。 我不知道如何让它像那样动画,但是一旦您检索数据,它就会进入您的$.get()函数。 此外,由于同源策略,这必须与 Ajax http://tzeale.com/位于同一域上


但是,在查看 minecraft.net 网站后,我注意到他们将这些数据加载到他们的页面中一次,而不是每 1 秒获取一次:

<script>
var justLoggedIn = false;
var totalUsers = 33652552;
var paidUsers = 6495707;
var totalUsersRate = 1.2166667;
var paidUsersRate = 0.15;
</script>

然后他们不会因此获得实时数据。 他们只是得到当前金额,然后继续添加 1。

他们使用此插件使其动画化:http://timeago.yarp.com/并且仍然使用 setTimeout() 每秒不断添加 1。我不认为这是真正的用户,只是一个从var totalUsers开始的计数器