The Load function provided by jQuery may be the most lightweight method to implement ajax functions. Today, I want to introduce a practical application of the load () function. I hope you will find it very simple and practical after reading it. The following is a tool similar to Kingsoft's back-to-words. It is similar to scrolling text (images), but ajax is used, that is, the execution of scripts on the server.
First, a text file package I created contains the English words I want to recite, and then the following PHP code is used to read the words and return a random word.
The Code is as follows:
$ Buffer = array ();
$ Handle = @ fopen ("toefl_listen.txt", "r ");
If ($ handle ){
While (! Feof ($ handle )){
Array_push ($ buffer, fgets ($ handle, 4096 ));
}
Fclose ($ handle );
}
Echo $ buffer [array_rand ($ buffer)];
?>
Finally, add Ajax technology to the following Javascript script to call the PHP code on the server and display the returned results in a specific DIV. Because it is loop playback, I used the setInterval () function. In addition, the clearInterval () function is used to implement the function of moving the mouse over-tentative playback.
The Code is as follows:
Script
$ (Document). ready (function ()
{
// Does not call the PHP file on the server every 3 seconds
Var refreshId = setInterval (function ()
{
$ ('# Timeval'). load ('reflesh. php ');
},3000 );
// Move the mouse over to pause playback
$ ("# Timeval"). mouseover (function ()
{
ClearInterval (refreshId );
});
$ ("# Timeval"). mouseout (function (){
RefreshId = setInterval (function ()
{
$ ('# Timeval'). load ('reflesh. php ');
},3000 );
});
});
Script
I think the above mentioned Code that calls the server for a certain period of time is quite scalable. I only use it to read a simple text file. You can also use it to call the database to update a data in real time.