Ajax WebGame,不断重新加载地图而不破坏其他页面

发布于 2024-11-03 14:22:07 字数 733 浏览 3 评论 0原文

我正在制作一个网页游戏,它的界面非常简单,由以下部分组成:

<div>logo</div>
<div id="map"></div>
<div>some buttons here</div>

碰巧的是,id =“map”的div是我的地图出现的地方,但游戏的其他部分也看起来太像库存了。 我使用 jQuery 来做 ajax 的事情,所以它是这样的:

$(document).ready(function() {$('#map').load('map.php');});

上面的代码第一次加载 div,然后当我单击方向按钮时,它会再次调用带有一些参数的函数,以便玩家可以移动,但这是问题,我想在我使用地图时保持地图重新加载。
我尝试做一个:

setInterval("$(document).ready(function() {$('#mapadiv').load('map.php');});",1000);

事实上它确实有效,但是当我单击一个更改 div 的按钮而不是地图(如库存)时,它会快速显示库存屏幕并再次更改为地图。
当然它会改变,因为它一直在调用地图!所以我想并尝试将“setInterval”放入Map.php中,这样它只会在屏幕上时调用,但这也不起作用,它与第一次尝试的效果相同。 我想我错过了一些东西,有人可以帮助我吗? 提前致谢

Im Making a Web Game, and its interface its very simple, composed of:

<div>logo</div>
<div id="map"></div>
<div>some buttons here</div>

Happens that, the div with id = "map" is where my map appears, but also other sections of the game appears too liek the inventory.
Im using jQuery to do the ajax stuff, so its like this:

$(document).ready(function() {$('#map').load('map.php');});

The above code load the div for the first time, then when i click a directional button, it calls again that function with some parameters so the player can move, but here's the problem, i'd like to keep the map reloading while im on it.
I Tried to do an:

setInterval("$(document).ready(function() {$('#mapadiv').load('map.php');});",1000);

and in fact it did worked, but when i clicked a button that changed the div and wasnt the map (like the inventory) it rapidly shows the inventory screen and changes again to the map.

Of Course it changes, because the it keeps calling the map! so i thought and tried to put that "setInterval" inside the Map.php so it would only call while its on the screen but that didnt worked too, it did the same as the first try.
I think im missing something, can anybody help me?
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2024-11-10 14:22:07

我假设您也有这个按钮:

   <input type="button" id="btnInventory" />
   <input type="button" id="btnRefreshMap" />

您的脚本应该是这样的:

function loadMap(){
  $('#map').load('map.php');
  $("#map").addClass("keepRefreshingMap");  //This will do the trick
}
function loadInventory(){
  $("#map").load("inventory.php"); //Put your loading inventory code here
  $("#map").removeClass("keepRefreshingMap");  //Remove the class so it doesn't refresh automatically
}
$(document).ready(function() {
   loadMap();
   $("#btnRefreshMap").click(function(){ loadMap(); });
   $("#btnInventory").click(function(){ loadInventory() });
   setInterval(function(){
      if($("#map").hasClass("keepRefreshingMap"))  //Only refresh if it has the class
          loadMap();
   }, 1000);

});

正如您所看到的,技巧就在 keepRefreshingMap 类中。只要地图div有它,就会不断刷新。当您打开清单时,此类将被删除,因此地图不会刷新。

希望这有帮助。

I'll asume you have this buttons also:

   <input type="button" id="btnInventory" />
   <input type="button" id="btnRefreshMap" />

Your script should be like this:

function loadMap(){
  $('#map').load('map.php');
  $("#map").addClass("keepRefreshingMap");  //This will do the trick
}
function loadInventory(){
  $("#map").load("inventory.php"); //Put your loading inventory code here
  $("#map").removeClass("keepRefreshingMap");  //Remove the class so it doesn't refresh automatically
}
$(document).ready(function() {
   loadMap();
   $("#btnRefreshMap").click(function(){ loadMap(); });
   $("#btnInventory").click(function(){ loadInventory() });
   setInterval(function(){
      if($("#map").hasClass("keepRefreshingMap"))  //Only refresh if it has the class
          loadMap();
   }, 1000);

});

As you can see, the trick is in the keepRefreshingMap class. As long as the map div has it, it will keep refreshing. When you open the inventory, this class is removed, so the map isn't refreshed.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文