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

时间:2016-12-30 06:39:37

标签: javascript php jquery ajax page-refresh

所以我基本上尝试在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();
    
  5. 我一直在玩这个逻辑,但它要么无限打印,要么根本不打印,要么撞坏我的服务器。由于担心再次崩溃服务器,我害怕更多地玩游戏。也许有人可以解释正确的方法,或者为什么它不可行,如果它不是?

    我很抱歉,如果这些都是愚蠢的问题,我花了几天时间研究解决这个问题的方法,我不确定我是否可以使用javascript或ajax以及我的字段来实现它打印功能,我尝试了一些,他们没有工作,我不确定它是否是因为我的方法是否可行。

1 个答案:

答案 0 :(得分:0)

您可以使用javascript或Ajax帮助div,您可以将id分配给div并在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/

谢谢

相关问题