繁体   English   中英

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

[英]how to refresh php div every few seconds

因此,我基本上是在尝试使用php和(可能是最少的)javascript实现在线交易卡游戏,到目前为止,我已经在php中建立了GUI,该数据库具有所需的表(到目前为止)以容纳甲板,字段以及其他各种数据。 我已经了解到了一个函数,该函数可以初始化两个玩家的字段,并使用牌组中的牌来填充其字段和手。 我还了解到另一台计算机或设备上的玩家可以获取游戏的房间号并加入游戏,以便查看确切的初始化字段。

我的下一个目标是能够设置转弯逻辑,并允许一个球员在另一个人在回合中进行比赛时更新屏幕。 我的问题是我不确定最小可行的解决方案是什么,因为我有很长的复杂函数根据字段表中的值显示当前字段。 因此,我的理想逻辑是这样设置的:

//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
}

我将可填充主机函数包装在一个可刷新的div中,如下所示:

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

以便每5秒左右调用一次该函数,但这只会导致该字段在5秒后消失。 所以...

  1. 为什么消失了?

  2. 如何使用刷新字段功能来刷新页面并仅显示该字段的一个实例?

  3. 这种方法可行吗? 如果没有,我能采取的最简单方法是什么?

  4. 我还尝试了一个解决方案,该函数每5秒调用一次该函数并删除该函数,但是我不确定我是否做对了,PHP手册并没有很清楚地解释它,并且在经过反复试验之后我想我崩溃了我的服务器。 就像这样:

    //环

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

我一直在尝试这种逻辑,但是它要么无限打印,要么根本不打印,或者崩溃了我的服务器。 我担心玩得更多,以免再次导致服务器崩溃。 也许有人可以解释这样做的正确方法,或者为什么这样做不可行?

很抱歉,如果这些问题很愚蠢,我已经花了很多天研究这个问题的方法,我不确定是否可以使用javascript或ajax以及我的字段打印功能来实现它,我尝试了一些,但是没有用,我不确定是否是因为我的方法可行。

您可以使用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)
})

检查这个小提琴:

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

谢谢

暂无
暂无

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

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