Php+jquery real-time display of the number of online Web sites, jquery online number
This paper describes the method of Php+jquery real-time display of the number of online website. Share to everyone for your reference. The specific analysis is as follows:
Online number of the simplest is the direct use of JS call PHP, so that can show how many people visited the site, if the user does not refresh the status of the page in real-time display users online, we can use jquery Ajax to achieve.
We need to dynamically display data in some applications, such as the current number of people online, current transaction totals, current exchange rates and so on, front-end pages need real-time refresh to get the latest data. Here we will combine examples to introduce the use of jquery and PHP to achieve dynamic digital display results.
This example assumes that the current number of online users is being displayed dynamically on the page (without having to refresh the entire page, just locally refreshing the dynamic number), and is commonly used on some statistical platforms. In an HTML page, you simply define the following structure:
Copy the Code code as follows: Current online:
First we define an animation process that uses jquery's animate () function to transform the process from one number to another, and the following magic_number () Custom function consolidates the code as follows:
[code]function Magic_number (value) {
var num = $ ("#number");
Num.animate ({count:value}, {
duration:500,
Step:function () {
Num.text (String (parseint (this.count))) ;
}
});
}; The
then the update () function uses jquery's $.getjson () to send an AJAX request to the background number.php, which, after getting PHP, calls Magic_number () to show the latest numbers. In order to see better results, we use setinterval () to set the time interval for code execution. The
Copy Code code is as follows: Function update () {
$.getjson ("number.php?jsonp=?", function (data) {
Magic_number (DATA.N);
});
};
setinterval (update, 5000);//5 Seconds to execute
Update ();
PHP Code section:
in the actual project, we will use PHP to get the latest data in the database, and then return to the front end via PHP. For a better demonstration, use random numbers, and finally return to the front-end js,number.php code in JSON format as follows:
Copy Code code is as follows: $total _data = Array (
' n ' = > rand (0,999)
);
Echo $_get[' Jsonp ']. ' ('. Json_encode ($total _data). ')'; The
principle is actually very simple is the use of JS settimeout implementation of a few seconds to load a PHP file to achieve real-time display of the number of people online function.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/937090.html www.bkjia.com true http://www.bkjia.com/PHPjc/937090.html techarticle Php+jquery Real-time display of the number of people online, jquery online This example describes the method of Php+jquery real-time display of the number of Web sites online. Share to everyone for your reference. Specific ...