增量输出-Jquery和PHP [英] Incremental Output - Jquery and PHP

查看:118
本文介绍了增量输出-Jquery和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以对大量项目进行评分,并为每个项目输出分数.

I have an application that rates a large set of items and outputs a score for each one.

在我的PHP脚本中,我使用ob_start和ob_flush处理每个等级的数据输出.如果我直接加载脚本,则效果很好.但是,当我尝试通过jquery使用.get时,将加载整个内容,然后将其放置在容器中,而不是逐步添加.

In my php script I'm using ob_start and ob_flush to handle the output of data for each rating. This works great if I directly load the script. But when I try to use .get via jquery, the entire content loads and then is placed into a container, instead of incrementally adding.

我想知道以下内容

  1. 是否可以在获取完成之前启动数据放置?
  2. 在过程完成之前,我是否需要不断轮询脚本?
  3. 是否有一种更有效的方式来显示数据而不是获取数据?

推荐答案

对于此类问题,我将采用以下方法:

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

  1. 对于在浏览器中禁用javascript的用户,请保留使用ob_start()ob_flush()的旧脚本.
  2. 对于启用了javascript的用户,一次加载一个预定义的数字内容.为了区分js是否启用用户,我在考虑2页.在第一页中,您显示指向旧脚本的链接.然后在此页面中放置一个jQuery代码以拦截对旧脚本的链接的单击,因此单击该链接将显示(或创建)div,然后将内容加载到该div.
  3. 您可以使用setTimeout连续调用AJAX代码,然后在达到特定条件(例如,空响应)后,可以使用clearTimeout删除setTimeout.每个AJAX请求都需要有一个偏移量参数,因此它将从上一个AJAX调用中获取内容.收到响应后,增加下一个AJAX调用的偏移量.您可以为此使用全局变量.
  4. 您可以使用一个简单的全局变量来阻止AJAX请求在最后一个AJAX仍在等待响应时运行,以防止出现竞争状况.示例代码:

  1. Keep the old script that using ob_start() and ob_flush() for a user that disable javascript in their browser.
  2. For a user that have javascript enable, load the predefined number content one at a time. To differentiate between js enable user and not, I'm thinking of 2 page. In first page you display a link to old script. Then put a jquery code in this page to intercept click on the link to old script, so click on that link will display (or create) a div, then load the content into that div.
  3. You can use a setTimeout to call AJAX code continuously, then after a certain condition reached (Ex, empty response), you can remove the setTimeout using clearTimeout. Each AJAX request will need to have an offset param, so it will fetch content from last AJAX call. After receive response, increment the offset for the next AJAX call. You can use global variable for this.
  4. You can use a simple global variable to prevent an AJAX request run while the last AJAX still waiting response, to prevent race condition. Example code:

//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;
      });
  }
}

必须注意的一点是使用AJAX调用必须手动确定响应,因为ob_startob_flush在这种情况下将无效.

The point you must pay attention that using AJAX call, you must determine the response manually, since ob_start and ob_flush will have no effect in this scenario.

我希望这将有助于您创建自己的代码.

I hope this will help you create your own code.

这篇关于增量输出-Jquery和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆