The waiting time for Ajax is extremely long compared to the download time. To give you an example, it would be something like 1.5 seconds for the wait and .2 seconds for the download. I’m guessing this means that my server-side script can be improved. Is there something I can do to improve this script? What’s causing the hold up?

<?php
/**
 * Ajax functions
 */
// Return the post content to the AJAX call
function my_load_ajax_content () {
    $args = array(
        'p' => $_POST['post_id'],
        'post_type' => 'projects'
        );
    $post_query = new WP_Query( $args );
    while( $post_query->have_posts() ) : $post_query->the_post(); ?>
    <div class="post-container">
        <div id="project-left-content">
            <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
            <?php the_content(); ?>
            <?php if( get_field('client') ): ?>
                <div class="client">
                    Client(s): <?php the_field('client'); ?>
                </div>
            <?php endif; ?>
            <div class="project-cats">
                <?php
                    $cat_names = wp_list_pluck( get_the_category(), 'cat_name');
                    echo join( ', ', $cat_names );
                ?>
            </div>
            <?php if( get_field('url') ): ?>
                <div class="project-link">
                    <a class="first after" href="http://<?php the_field('url'); ?>" target="_blank"><?php the_field('url'); ?></a>
                </div>
            <?php endif; ?>
        </div>
        <div id="project-right-content">
            <?php if( have_rows('slides') ): ?>
                <div id="slider">
                    <!-- Slider Setup -->
                    <?php if( have_rows('slides') ):
                        $slideNumber = 0;
                        while ( have_rows('slides') ) : the_row();
                        $slideNumber++;
                    ?>
                        <input type="radio" name="slider" id="slide<?php echo $slideNumber; ?>">
                    <?php endwhile;endif; ?>
                    <!-- Slide -->
                    <?php if( have_rows('slides') ): ?>
                        <div id="slides">
                            <div id="overflow">
                                <div class="inner">
                                    <?php if( have_rows('slides') ):
                                    while ( have_rows('slides') ) : the_row();
                                        $slideImage = get_sub_field('slide_image');
                                    ?>
                                    <article>
                                        <img src="<?php echo $slideImage; ?>" alt="<?php the_title(); ?>">
                                    </article>
                                    <?php endwhile;endif; ?>
                                </div><!-- #inner -->
                            </div><!-- #overflow -->
                        </div><!-- #slides -->
                    <?php endif; ?>
                    <!-- Controls -->
                    <?php if( have_rows('slides') ):
                        $slideNumber = 0;
                    ?>
                        <div id="active">
                            <?php while ( have_rows('slides') ) : the_row();
                                $slideNumber++;
                            ?>
                                <label for="slide<?php echo $slideNumber; ?>"></label>
                            <?php endwhile; ?>
                        </div><!-- #active -->
                    <?php endif; ?>
                </div><!-- #slider -->
            <?php endif; ?>
        </div><!-- #project-right-content -->
    </div><!-- .post-container -->
    <?php
    endwhile;
    wp_die();
}
add_action ( 'wp_ajax_nopriv_load-content', 'my_load_ajax_content' ); // when the user is logged in
add_action ( 'wp_ajax_load-content', 'my_load_ajax_content' ); // when the user is not logged in

Read more here: Getting slow Ajax waiting time (TTFB)


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: