Incremental Output - JQuery and PHP

I have an application that evaluates a large set of elements and displays a rating for each of them.

In my php script, I use ob_start and ob_flush to handle the output of data for each rating. This works fine if I directly load the script. But when I try to use .get through jquery, all the content is loaded and then placed in a container, rather than being gradually added.

I am interested in the following

  • Is there a way to initiate data placement before get completed?
  • Do I need to constantly check the script until the process is complete?
  • Is there a more efficient way to display data instead of retrieving?
+1
source share
3 answers

For this kind of problem, I will have this approach:

  • Save the old script that uses ob_start() and ob_flush() for a user who disables javascript in their browser.
  • For a user with javascript enabled, upload a predefined amount of content one at a time. To distinguish between js enable user and not, I am thinking of 2 page. The first page displays a link to the old script. Then put the jquery code on this page to intercept the link to the link to the old script, so click on this link to display (or create) a div , and then load the contents into a div .
  • You can use setTimeout to continuously call the AJAX code, and then after reaching a certain condition (Ex, an empty answer), you can delete setTimeout with clearTimeout . Each AJAX request must have an offset parameter, so it will receive content from the last AJAX call. After receiving the response, increase the offset for the next AJAX call. You can use a global variable for this.
  • You can use a simple global variable to prevent an AJAX request from being executed during the last AJAX response to prevent a race condition. Code example:

      // lock variable
     var is_running = FALSE;
     // offset start with 0
     var offset = 0;
     function load_content ($) {
       // check lock
       if (! is_running) {
         // lock
         is_running = true;
         // do AJAX
         $ .get (URL,
           {PARAM},
           function (resp) {
             // put data to 'div'
             // ...
             // if empty, then call clearTimeout
             // ...
             // increase offset here
             offset = offset + NUM_ITEM_FETCHED
             // release lock
             is_running = false;
           });
       }
     } 

It should be noted that when using the AJAX call, you must determine the answer manually, since ob_start and ob_flush will not have an effect in this scenario.

Hope this helps you create your own code.

+2
source

JQuery will get success status from ajax call when the full page finishes loading .. so that everything you do in php will not be returned to the calling page until the whole process is complete .. (ajax is one-send / one-receive )

You will need to complicate your system in order to do what you want.

example..

  • your php updates the external progress file, and your jquery polls this file at some interval and displays the progress.
    You must initiate interval polling on ajax submit, and on ajax submit, stop it.
+1
source

I had a similar problem a while ago when I wanted a php script to send a series of emails and refresh the jquery page to say something like β€œSubmit 23/50”.

What I ended up with is setting up a php script to process one element at a time. This may also work in your case. Could you jquery pass the identifier of an element of some kind to a PHP script that processes only one element? Then in the callback you can put the data for this element on the page, as well as create a new ajax request for the next element. In other words, each callback created a new query for the next item until the entire list of items was looped.

What do you think?

-DLH

+1
source

Source: https://habr.com/ru/post/1389737/


All Articles