防止jQuery AJAX调用等待上一个调用完成 [英] Prevent jQuery AJAX call from waiting for previous call to complete

查看:116
本文介绍了防止jQuery AJAX调用等待上一个调用完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了SO,似乎每个问题都在询问如何等待 完成AJAX调用.我问如何不等待完成AJAX调用.

I've searched SO, and every question seems to be asking how to wait for an AJAX call to complete. I am asking how not to wait for an AJAX call to complete.

我有一个电子商务网站,该网站通过PHP进行了一些繁重的图像处理.该操作是通过AJAX调用触发的.

I have an e-commerce site that does some heavy image manipulation via PHP. The manipulation is triggered via AJAX calls.

我正试图加快用户体验,因此我修改了代码,以首先使PHP呈现缩略图(该操作很快完成),然后触发第二个AJAX调用,告诉PHP呈现完整图像(可以需要几分钟).

I am trying to speed up the user experience, so I have modified the code to first have PHP render a thumbnail (the operation completes quickly), then trigger a second AJAX call that tells PHP to render the full image (which can take a few minutes).

添加到购物车"操作也是AJAX调用. 问题是,直到上一个AJAX调用完成后,添加到购物车"调用才能完成.

The "Add To Cart" operation is also an AJAX call. The problem is that the "Add to Cart" call is unable to complete until the previous AJAX call is completed.

序列:

  1. AJAX调用A生成请求缩略图.
  2. 呼叫A的成功回调:
    一种.显示/启用添加到购物车按钮"
    b.触发AJAX调用B,以生成完整图像.
  3. 点击添加到购物车"会触发AJAX调用C,直到调用B完成,该调用才会完成.
  1. AJAX call A requests thumbnail be generated.
  2. The success callback for call A:
    a. Displays / enables the "Add to Cart button"
    b. Triggers AJAX call B, for the full image to be generated.
  3. Clicking "Add to Cart" triggers AJAX call C, which does not complete until call B completes.

相关的javascript:

Relevant javascript:

/** Call A - make call for thumbnail generation **/
$.ajax({
    url: 'index.php?route=decorable/image/thumbnail',
    type: 'post',
    data: image_data,
    dataType: 'json',
    success: function(json) {
        if (json['success']) {
            $('#button-cart').show();
            /** Call B - make call for *full* layout generation **/
            $.ajax({
                url: 'index.php?route=decorable/image',
                type: 'post',
                data: image_data,
                dataType: 'json'
             });
    });

/** Call C - this AJAX call starts, but does not complete, until call B completes **/
$('#button-cart').click(function() {
    $.ajax({
        url: 'index.php?route=checkout/cart/add',
        type: 'post',
        data: cart_data,
        dataType: 'json',
        success: function(json) { 
            if (json['success']) {      
                $('.success').fadeIn('slow');
            }
        }
    });
});

我是误会了吗,或者即使呼叫B未完成,呼叫C应该能够完成吗?

如果您相信,PHP可以维持这种体验,那么有什么好方法让我触发PHP开始执行,但仍将响应发送回缩略图AJAX调用?

If you believe that the PHP is holding up the experience, then is there a good way for to me to trigger PHP to begin executing, but still send the response back for the thumbnail AJAX call?

推荐答案

以我的经验,这是由使用PHP会话引起的,当您确定在代码(所有ajax请求)中不必修改会话时那么您应该致电:

In my experience this was caused by PHP sessions been used, when you're sure in the code (all ajax requests) that you will not have to modify sessions then you should call:

session_write_close()

这将允许同时进行其他请求.

This will then allow other requests to be made simultaneously.

进一步阅读: http://www.php.net/manual/zh-CN/function.session-write-close.php

这篇关于防止jQuery AJAX调用等待上一个调用完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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