多次调用AJAX的页面(有时)花费的时间太长

时间:2019-06-25 16:33:45

标签: php ajax

我会尽量做到透彻。

我有一个使用codeigniter框架用PHP编写的应用程序。

在少数几个页面上,我有很多信息要显示,因此,将其全部拆分为多个Ajax调用是一个好主意。至少在过去两年中一直是这样,但最近我的客户抱怨速度问题。

我已经测试了页面,并对其进行了6/10次加载而没有出现问题。

该页面有6个ajax调用,如图所示,大多数运行时间不到500毫秒。

enter image description here

但是,在大约4/10页重新加载时,它会挂起,导致至少2个ajax调用在5秒内完成,有时甚至长达20秒。

enter image description here

这是一段代码:

    $('#table_booking_details').dataTable({
    "autoWidth": false,
     "responsive" : {
        "details": false,
    },
    "ajax": {
        "url"  : "/admin/booking/dt_get_rental_date_times/",
        "type" : "POST",
        "data" : {
            "rental_index" : $('#rental_index').val() 
        }
    },
    "serverSide" : true,
    "processing" : true,
});


    public function dt_get_rental_date_times()
{
    $rental_index       = $this->input->post('rental_index');
    $search             = $this->input->post('search');
    $search_param       = $search['value'];

    $sort               = $this->input->post('order');
    $sort_column_index  = $sort['0']['column'];
    $sort_direction     = $sort['0']['dir'];

    $length             = $this->input->post('length');
    $start              = $this->input->post('start');

    $sort_column_array  = array("0" => "collection_date_time", "1" => "return_date_time", "2" => "vehicle_registration", "3" => "(return_date_time - collection_date_time)", "4" => "rental_date_time_cost");

    $sort_column        = $sort_column_array[$sort_column_index];


    $rental_date_times_result = $this->rentals_model->dt_get_rental_date_times($length, $start, $sort_column, $sort_direction, $search_param, $rental_index);

    $rental_date_times          = $rental_date_times_result['rows'];
    $rental_date_times_count    = $rental_date_times_result['num_rows'];

    $data['recordsTotal']        = $rental_date_times_count;
    $data['recordsFiltered']     = $rental_date_times_count;
    $results                     = array();

    $i = 0;

    foreach ($rental_date_times as $rent) 
    {           
        $days = ceil(($rent->return_date_time - $rent->collection_date_time) / 60 / 60 / 24);

        $actions = '';

        if($rent->swapover == 1)
        {
            $actions = 'Yes';
        }
        else
        {
            $actions = 'No';
        }

        $results[] = array(
                "DT_RowId"      => "rentalperiod_" . $rent->rental_date_time_index,
                "DT_RowClass"   => 'linkable_row',
                "0"             => timestamp_to_date_time($rent->collection_date_time),
                "1"             => timestamp_to_date_time($rent->return_date_time),
                "2"             => $rent->vehicle_registration,
                "3"             => $days,
                "4"             => $rent->rental_date_time_cost,
                "5"             => $actions
        );
    }

    $data['data'] = $results;

    echo json_encode($data);
}

首先,这很可能是人们刚刚开始注意到的新事物,似乎有点奇怪,它会随机开始发生。

任何建议,我可以做些什么来改进它,而不必通过重新加载页面将所有内容都移回老式php。

谢谢

0 个答案:

没有答案