2

によって生成された主に動的コンテンツを含むページがありますAjax。30 秒ごとにランダム化され、データベースからの新しいコンテンツが表示されます。問題ないようにPHP見えますが、コードに問題があるか、データベースに問題があるために遅延が発生しているようです( AjaxJavascriptのロードには約 30 秒かかります!)関数を実行するに割り当てられたミリ秒待機していますが、Javascript にエラーが見つかりません。また、データベースから取得された 2 つの画像 URL 文字列があり、外部ソースから情報を取得する必要があるときに Ajax が遅れることはもっともらしいことです。または、PHP構文の使用に遅れがあるのか​​もしれません。

setInterval



MySQLORDER BY rand()?

関連するhtmlは次のとおりです。

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

関連する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();
    }

...そして、関連する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
?>
4

2 に答える 2

3

onload メソッド内でコードを実行していません。AJAX セットアップ コードがページの読み込みよりも速く実行される可能性があるため、var products = ...null です。次のようなことをする必要があります。

<body onload='setIntervalOnload();'>

また

 window.onload = setIntervalOnload;
于 2012-04-18T02:59:14.443 に答える
0

setInterval関数はすぐには呼び出されません。関数の最初の実行は、間隔がいくらか遅れます。

タイマーだけでなくすぐに実行する場合は、関数を自分で呼び出す必要があります。

于 2012-04-18T02:03:13.717 に答える