Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7830447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:14:06+00:00 2026-06-02T11:14:06+00:00

I have a page that has primarily dynamic content generated by Ajax . It

  • 0

I have a page that has primarily dynamic content generated by Ajax. It is randomized every 30 seconds, and new content from the database appears. The PHP seems fine, but there either seems to be something wrong with my Javascript code or there is a problem on the database causing it to lag (the Ajax takes about 30 seconds to load!)

It would seem as if the recursive call to setInterval in my Javascript is waiting the alloted milliseconds before executing the function, but I can’t find any errors in my Javascript.

Also, there are two image url Strings retrieved from the database, and it seems plausible that Ajax would lag when it has to retrieve information from external sources.

Or maybe is there a lag in my usage of the PHP-MySQL syntax ORDER BY rand()?

Here is the relevant html:

    <html>
    <head>
    <title></title>
    <script type = "text/javascript" src = "randomProducts.js" />
    <script type = "text/javascript">
    setIntervalOnload();
    </script>
    </head>
    <body>
    ...
    ...
    ...
    </body>
    </html>

Here is the relevant Javascript:

    // global static variables
        var subCategory; // initialized from setCategoryTree
        var t; // for setInterval and clearInterval

        var seconds;
        var millisecondsPerSecond;
        var milliseconds;

    function setIntervalOnload()
    {
        getRandomProducts();
        if(typeof t != "undefined")
            clearInterval(t);

        seconds = 30;
        millisecondsPerSecond = 1000;
        milliseconds = seconds * millisecondsPerSecond;

        t = setInterval(getRandomProducts, milliseconds);
    }

    function getRandomProducts()
    {
        //window.alert(subCategory);
        if(typeof subCategory == "undefined")
            subCategory = "all";
        else
        {
            clearInterval(t);
            t = setInterval(getRandomProducts, milliseconds);
        }

        var req = new XMLHttpRequest();

        var products = document.getElementById("products");

        req.onreadystatechange = function()
        {
            if( (req.readyState == 4) && (req.status == 200) )
            {
                var result = req.responseText;
                products.innerHTML = result;
            }
        };
        req.open("GET", "randomProducts.php?category=" + subCategory, true);
        req.send(null);
    }
    function setCategoryTree(link)
    {
        var categoryTree = document.getElementById("categoryTree");

        /* climbing the DOM-tree to get the category name (innerHTML of highest "a" tag) */
        var category = link.parentNode.parentNode.parentNode.getElementsByTagName("a")[0].innerHTML;

        subCategory = link.innerHTML;

        categoryTree.innerHTML = "==>&nbsp;" + category + "&nbsp;&nbsp;==>&nbsp;" + subCategory;

        getRandomProducts();
    }

…and here is the relevant PHP:

<?php

    // connect to MySQL
    $dbName = "blah";
    $db = mysql_connect("localhost", $dbName, "asdf");
        if (!$db)
        {
             echo "<p>Error - Could not connect to MySQL</p>";
             exit;
        }

    // select the blah database
    $blah = mysql_select_db("blah");
        if(!$blah)
        {
            echo "<p>Error  - Could not select the blah database.</p>";
            exit;
        }

    // fix html characters in $subCategory
    $subCategory = $_GET["category"];
    trim($subCategory);
    $subCategory = stripslashes($subCategory);
    $subCategoryFixed = htmlspecialchars($subCategory);

    // for loop for each random product (total of 10 random products)
    for($i = 0; $i < 10; $i++)
    {
        // query the blah database for all products
        if($subCategoryFixed == "all")
        {
            $query = "SELECT * FROM products ORDER BY rand();";
            $result = mysql_query($query);
        }
        else // query the blah database for products in selected subCategory
        {
            $query = "SELECT * FROM products WHERE cat = " . $subCategoryFixed . " ORDER BY rand();";
            $result = mysql_query($query);
        }
            if (!$result)
            {
                echo "<p>Error - the query could not be executed</p><br />";
                $error = mysql_error();
                echo "<p>" . $error . "</p>";
                exit;
            }

        $row = mysql_fetch_array($result);
        $productValues = array_values($row);

        $name = htmlspecialchars($productValues[3]);
        $price = htmlspecialchars($productValues[5]);
        $img = htmlspecialchars($productValues[7]);

        // product info is formatted for home.html here
        $str = '<div>
                    <a href = "' . $link . '" title = "' . $name . '">
                        <table id = "product-table" onmouseover = "darkenProduct(this);" onmouseout = "lightenProduct(this);">
                            <tr>
                                <td>' . $name .'</td>
                            </tr>
                            <tr>
                                <td><img src = "' . $img . '" /></td>
                            </tr>
                            <tr>
                                <td>' . $price . '</td>
                            </tr>
                        </table>
                    </a>
                </div>';
        echo $str;
    } // end of products for loop
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:14:14+00:00Added an answer on June 2, 2026 at 11:14 am

    You are not running your code inside of onload method. It is possible that the AJAX setup code runs faster than the page can load so var products = ... is null. You need to do something like the following:

    <body onload='setIntervalOnload();'>
    

    or

     window.onload = setIntervalOnload;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page that has an iframe From one of the pages within
I have a page that has several ajax submission forms. Each form has a
I have a Page that has a single instance of a UserControl that itself
I have a page that has fields dynamically loaded via JQuery. It allows a
I have a page that has 2 columns of words, 20 total, that are
I have a page that has a bunch of user controls on it. I
I have a page that has a large image on it with a number
I have a page that has 3 variables. They look like this: String[] Headers
I have a page that has pretty heavy (mid-weight rather) canvas operations going on.
I have a page that has an image in it and I styled it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.