简体   繁体   English

如何每隔几秒钟刷新一次php div

[英]how to refresh php div every few seconds

So I'm basically trying to implement an online trading card game in php and (maybe minimal) javascript, and so far I have a GUI set up in php, a database with the required tables (so far) to hold the decks, fields, and other various data. 因此,我基本上是在尝试使用php和(可能是最少的)javascript实现在线交易卡游戏,到目前为止,我已经在php中建立了GUI,该数据库具有所需的表(到目前为止)以容纳甲板,字段以及其他各种数据。 I've got it to the point where i have a function that initializes the fields for both players and populates their fields and hands with cards from their deck. 我已经了解到了一个函数,该函数可以初始化两个玩家的字段,并使用牌组中的牌来填充其字段和手。 I also got it to the point where a player on another computer or device can take the room number of the game and join the game so the see the exact initialized field. 我还了解到另一台计算机或设备上的玩家可以获取游戏的房间号并加入游戏,以便查看确切的初始化字段。

my next objective was to be able to set up the turn logic, and allow the screen to update for one player while the other is making plays during their turn. 我的下一个目标是能够设置转弯逻辑,并允许一个球员在另一个人在回合中进行比赛时更新屏幕。 My problem is I'm not sure what the minimum viable solution will be for this, because I have long intricate functions that display the current field based off of values that come from a field table. 我的问题是我不确定最小可行的解决方案是什么,因为我有很长的复杂函数根据字段表中的值显示当前字段。 So my ideal logic is set up like this: 因此,我的理想逻辑是这样设置的:

//find game form

//host game form

//if host is set (post){
  //call insert field function (a long function that creates a table for the current game being played and initializes the field and hand with random cards from the deck table)  
  //link game to host for future field printing
  // populate host function (a function that prints a table of every card on the field with the most up to date variables to represent each spot) 
}

//if find is set (post){
  //call insert field function
  //link game to host for future field printing
  //a button form to start the game which begins by calling heads or tails
  // populate find function (same as host but for find player side)
}

//if start game is set (post){
  // do game logic such as coin flip which will be notified to opponent in a message field in the game table upon the refresh
}

I was wrapping the populate host function in a refreshable div like this: 我将可填充主机函数包装在一个可刷新的div中,如下所示:

print "<div><meta http-equiv='refresh' content='5' />"; //host populate just disapears
populatehost($roomnumber); //populates self and opponents in hosts perspsective
print "</div>";

so that the function would be called every 5 seconds or so, but this just causes the field to disappear after 5 seconds. 以便每5秒左右调用一次该函数,但这只会导致该字段在5秒后消失。 So... 所以...

  1. Why is it disappearing? 为什么消失了?

  2. How can i get it to refresh the page and just display one instance of the field using the populate field function? 如何使用刷新字段功能来刷新页面并仅显示该字段的一个实例?

  3. Is this approach viable? 这种方法可行吗? if not, what's the easiest approach I could take and how? 如果没有,我能采取的最简单方法是什么?

  4. I also tried a solution where the function was called every 5 seconds and gotten rid of, but i'm not sure if I was doing it right, the PHP manual didn't explain it very clearly, and after lots of trial and error i think i crashed my server. 我还尝试了一个解决方案,该函数每5秒调用一次该函数并删除该函数,但是我不确定我是否做对了,PHP手册并没有很清楚地解释它,并且在经过反复试验之后我想我崩溃了我的服务器。 It was something like this: 就像这样:

    //loop //环

     ob_start(); populatehost($_SESSION['gamenumber']); sleep(5); ob_end_clean(); 

I kept playing around with that logic but it either printed infinitely, didn't print at all, or crashed my server. 我一直在尝试这种逻辑,但是它要么无限打印,要么根本不打印,或者崩溃了我的服务器。 I'm afraid to play around even more for fear of crashing the server again. 我担心玩得更多,以免再次导致服务器崩溃。 Maybe someone can explain the correct way to do it or why it is not viable if it isn't? 也许有人可以解释这样做的正确方法,或者为什么这样做不可行?

I'm sorry if these are dumb questions, I've spent days and days researching approaches to this problem, I wasn't sure if I could implement it using javascript or ajax along with my field printing function, I tried some and they weren't working, and I'm not sure if it's because my approach is viable or not. 很抱歉,如果这些问题很愚蠢,我已经花了很多天研究这个问题的方法,我不确定是否可以使用javascript或ajax以及我的字段打印功能来实现它,我尝试了一些,但是没有用,我不确定是否是因为我的方法可行。

You can take javascript or Ajax help for div you can assign id to the div and call id in javascript: 您可以使用div的javascript或Ajax帮助,您可以为div分配ID并在javascript中调用ID:

jQuery(function () {
    var $els = $('div[id^=quote]'),
        i = 0,
        len = $els.length;

    $els.slice(1).hide();
    setInterval(function () {
        $els.eq(i).fadeOut(function () {
            i = (i + 1) % len
            $els.eq(i).fadeIn();
        })
    }, 2500)
})

check this fiddle: 检查这个小提琴:

http://jsfiddle.net/arunpjohny/asTBL/ http://jsfiddle.net/arunpjohny/asTBL/

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM