DragonWar% 2016-02-07 20:44 采纳率: 0%
浏览 22

循环内的Ajax

I have some troubles using ajax within a loop. The loop and ajax work fine on their own, but combining them the trouble starts.

Below is a test script I wrote to see if the script works before I make the rest. The idea is that java script gets $numbers from the database and performs a loop for the amount it got (so in the example 7 times).
Each time ajax has to send the $numbers to php to search in the database for a %color corresponding with the value of $numbers, but since this is a test I just took "check" instead. This is then used to target a specific div which corresponds to the value of $numbers en $color (in this case $number and check).

At the moment I've tested a while loop and ajax separately and they work perfect, but combining them is where it goes wrong. The while ajax script will do nothing but freeze the browser.
I've also tried a for loop but this does nothing except giving me a Uncaught TypeError: Cannot set property 'innerHTML' of undefined.

This is the test script.

<?php
$number = 7;
?><?php
if(isset($_POST["number"])){
    echo 'Check';
    exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<script src="../Scripts/main.js"></script>
<script>
    function testwhile() {
        var number = <?php echo $number ?>;
        while (number > 0) {
            var x = document.getElementsByClassName(number);
            x[0].innerHTML = "Check!";
            var number = number - 1;
        }
    }
    function testajax() {
        var number = <?php echo $number ?>;
        var colour = "check";
        var ajax = ajaxObj("POST", "test1.php");
            ajax.onreadystatechange = function () {
                if (ajaxReturn(ajax) == true) {
                    var colour = ajax.responseText;
                    var x = document.getElementsByClassName(colour + ' ' + number);
                    x[0].innerHTML = "Check!";
                }        
            }
            ajax.send("number=" + number); 
    }
    function testwhileajax(){
        var number = <?php echo $number ?>;
        while (number > 0) {
            var colour = "check";
            var ajax = ajaxObj("POST", "test1.php");
            ajax.onreadystatechange = function () {
                if (ajaxReturn(ajax) == true) {
                    var colour = ajax.responseText;
                    var x = document.getElementsByClassName(colour + ' ' + number);
                    x[0].innerHTML = "Check!";
                    var numbers = numbers - 1;
                }        
            }
            ajax.send("number=" + number); 
        }
    }
    function testloopajax(){
        for (number = <?php echo $number ?>; number > 0; number--){
            //var number = number;
            var colour = "check";
            var ajax = ajaxObj("POST", "test1.php");
            ajax.onreadystatechange = function () {
                if (ajaxReturn(ajax) == true) {
                    var colour = ajax.responseText;
                    var x = document.getElementsByClassName(colour + ' ' + number);
                    x[0].innerHTML = "Check!";
                }        
            }
            ajax.send("number=" + number); 
        }
    }
</script>
</head>
<body>
    <div onclick="testajax()">Check ajax!</div>
    <div onclick="testwhile()">Check while!</div>
    <div onclick="testwhileajax()">Check both!</div>
    <div onclick="testloopajax()">Check loop!</div>
    <div class="1 Check">Check?</div>
    <div class="2 Check">Check?</div>
    <div class="3 Check">Check?</div>
    <div class="4 Check">Check?</div>
    <div class="5 Check">Check?</div>
    <div class="6 Check">Check?</div>
    <div class="7 Check">Check?</div>
</body>
</html>

Any help is much appreciated. :)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私