JQuery implementation refresh function code

In web development, it is often necessary to refresh a page or a partial data. At this time, you need to use the timed refresh to achieve it. The implementation is to request a data using the JS SetInterval function, and then return the request results to the front-end HTML to refresh.

The code is as follows:

   $ (Function () {getData (); setInterval (function () { GetData ();}, 3000);}); functiongetdata () {$ .Getjson ("/ blood / pressure / 1", function (data) {== data.code) {$ ("# systolic" ) .Text (Data.Data.SyStolic); $ ("# diastolic"). Text (data.data.diastolic); $ ("# pulse"). Text (data.data.pulse);}});}     Code Explanation: 
1. Import jQuery

    Use the above statement to import jQuery libraries, the following code requires support for the jQuery library.  2. Page loading starter 

$ (Function () {getData (); // First load data // Time task The time interval is 3000 ms. SetInterval (function () {GetData ();}, 3000);});

General timing tasks need to be started after the page load is completed. The page load has two events. First, ready, indicating that the document structure has been loaded (not included with non-text media files such as pictures), and the other is onload, indicating that the page contains a picture and other files, all the elements, etc., all the elements are loaded (can be said) : Ready triggers before OnLoad).
It is recommended to implement timing tasks when Ready, implement the following code:
   
$ (function () {// do some};

The above code is equivalent to:

 $ (document) .ready (function () {// DO Something})   
3. Get data and refresh the page
Using the following code to obtain the data and set the value of the page. Thus refreshing page data. This step writes the corresponding code based on your needs.

functiongetdata () {$ .Getjson (“/ blood / pressure / 1″, function (data) {if (200 == Data.code) {$ (” #nstolic “). Text (Data.Data.SyStolic); $ (” # diastolic “). Text (data.data.diastolic); $ (” # pulse “). Text (data.data.pulse);}} );};
   
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment