jQuery + PHP + Ajax statistics show examples of dynamic digital

jQuery + PHP + statistics show an example of a dynamic digital Ajax implementation, in this case is on the page dynamically shows the current number of online users, of course, you can apply to other more scenes.

First, we want to place the figures in the statistics #number:

<div class="count">当前在线:<span id="number"></span></div>


Then we define an animation process, using jQuery animate () function to achieve a transformation from the digital to another process number, the magic_number () custom function code is as follows:

1 function magic_number(value) {  
2     var num = $("#number");  
3     num.animate({count: value}, {  
4         duration: 500,  
5         step: function() {  
6             num.text(String(parseInt(this.count)));  
7         }  
8     });  
9 };


Then update () function uses jQuery's $ .get () ajax.php send an ajax request to the background, after obtaining the appropriate PHP, call magic_number () showcase the latest figures. In order to be able to see better results, we use the setInterval () is performed once every three seconds.

1 function update() { 
2     $.get("ajax.php", 
3     function(data) { 
4         magic_number(data); 
5     }); 
6 } 
7 setInterval(update, 3000); 
8 update();


We randomly selected a number from 0-999, you can read from the database table:

echo  mt_rand (0.999);


This switched: https://www.sucaihuo.com/php/117.html please indicate the source!

Guess you like

Origin www.cnblogs.com/mrlime/p/12095112.html