For循环导致页面崩溃

时间:2017-11-23 20:21:44

标签: javascript php ajax loops

的index.php

<?php
    require_once("inc/db.php");
    require_once("inc/map.php");

    $db = new Database();
    $map = new Map($_POST['x'], $_POST['y']);
    $map->createMap();
    $map->battle();
    $map->move();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
</script>
</head>
<body>

</body>
</html>

db.php中

<?php
    class Database{
        private $config;
        protected $sql;

        public function __construct(){
            $this->Connect();
        }

        public function Connect(){
            $this->config  = parse_ini_file("config.ini");
            $con = $this->config;
            $this->sql = new PDO("mysql:host={$con['host']}; dbname={$con['dbname']}",
            $con['username'],$con['password']);
            $this->sql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
    }
?>

Map.php

<?php 
    class Map extends Database{

      public $x;
      public $y;
      public $width = 5;
      public $height = 5;

      public function __construct($x,$y){
            $this->x = $x;
            $this->y = $y;
      }

      public function createMap(){
          for($x=0; $x < $this->width; $x++){
              for($y=0; $y < $this->height; $y++){
                  Database::Connect();
                  echo "<div id='map'>";
                  echo "<div class='map_columns' title='{$x}_{$y}' data-x='{$x}' data-y='{$y}' id='{$x}_{$y}'>";
                  $this->spawnPlayers($x,$y);
                  echo "</div>";
              }
              echo "<div class=\"break\"></div>";
          }
      }

      public function spawnPlayers($x,$y){
          $stmt = $this->sql->prepare('SELECT * FROM user WHERE x = "'.$x.'" AND y = "'.$y.'"');
          $stmt->execute();
          while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){
              echo "<img title='{$rows['name']} \n {$rows['x']}|{$rows['y']}'>";
          }
      }

      public function battle(){
          $stmt = $this->sql->prepare("SELECT * FROM user GROUP BY x,y HAVING count(*) > 1");
          $stmt->execute();
          $rows = $stmt->fetch(PDO::FETCH_ASSOC);
          if($rows){
              echo "<input type='submit' id='battle-btn' value='Fight'>";
          } else{
          }
       }

       public function move(){
         if(isset($this->x) && isset($this->y)){
            $stmt = $this->sql->prepare("UPDATE user SET x = '$this->x', y = '$this->y' WHERE name = 'Wiz'");
            $stmt->execute();
         }
       }
    }
?>

Index.js

$(document).ready(function(){
    $(document).on("click", ".map_columns", function(){
        var x = $(this).attr("data-x");
        var y = $(this).attr("data-y");
        $.post('./index.php', {x: x, y: y}, function(){

        });
    });

    setInterval(function(){
        $("#map").load("./index.php");
    }, 1000);
});

所以我使用for循环制作了一个地图,输出的数据是用户坐标。无论我在地图上按什么,它都会将用户坐标更新到该位置。这按预期工作,但我有一个问题。当用户坐标更新时,我需要地图自动刷新/更新。

所以我的方法是在大多数项目中使用ajax中的.load(),但这里的区别是for循环。我假设因为它第二次重新输入for循环,它会导致一切崩溃。我怎么解决这个问题?我知道游戏也做了同样的事情,但我无法想到反制解决方案。编写的代码非常容易理解。

我重新编写了原始版本,因此它对于stackoverflow来说很干净且易于理解,原始版本如下所示:

https://i.gyazo.com/a34793a0af0344b867f0537830da5cd3.mp4

使用不可行的html进行自动更新。那么请问如何在没有页面崩溃的情况下用新信息(玩家轴)更新div?

1 个答案:

答案 0 :(得分:1)

问题是您将整个页面加载到#map DIV中,但您只想替换自己的内容。将其更改为:

    $.get("index.php", function(response) {
        $("#map").replaceWith($(response).find("#map"));
    });

创建一个只返回所需内容的新脚本可能会更好,而不是使用index.php然后忽略所有其他内容。