Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何每隔几秒钟刷新一次php div_Javascript_Php_Jquery_Ajax_Page Refresh - Fatal编程技术网

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

Javascript 如何每隔几秒钟刷新一次php div,javascript,php,jquery,ajax,page-refresh,Javascript,Php,Jquery,Ajax,Page Refresh,因此,我基本上是在尝试用php和(可能是最小的)javascript实现一个在线交易卡游戏,到目前为止,我已经用php建立了一个GUI,一个数据库,其中包含所需的表(到目前为止)来保存牌组、字段和其他各种数据。我已经做到了,我有一个函数可以初始化两个玩家的场,并用他们牌组中的牌填充他们的场和手。我还让另一台计算机或设备上的玩家可以获取游戏的房间号并加入游戏,这样玩家就可以看到准确的初始化字段 我的下一个目标是能够设置回合逻辑,并允许一名球员在另一名球员轮换时更新屏幕。我的问题是,我不确定这个问题

因此,我基本上是在尝试用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
}
print "<div><meta http-equiv='refresh' content='5' />"; //host populate just disapears
populatehost($roomnumber); //populates self and opponents in hosts perspsective
print "</div>";
我将填充主机函数包装在一个可刷新的div中,如下所示:

//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
}
print "<div><meta http-equiv='refresh' content='5' />"; //host populate just disapears
populatehost($roomnumber); //populates self and opponents in hosts perspsective
print "</div>";
我一直在玩弄这种逻辑,但它要么无限打印,要么根本不打印,要么使我的服务器崩溃。我害怕玩得更多,因为我害怕服务器再次崩溃。也许有人可以解释正确的方法,或者如果不可行,为什么不可行


如果这些都是愚蠢的问题,我很抱歉,我已经花了很多天研究解决这个问题的方法,我不确定是否可以使用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)
})
检查这把小提琴:


谢谢

很抱歉,我对jquery不太熟悉。我必须在php文件中的何处打印此函数才能使其工作?为了让jquery正常工作,我需要在文件的顶部导入任何东西吗?你需要启动脚本标记,然后添加上面的脚本代码,然后关闭脚本,你需要添加代码,在哪个文件中添加你的div,哪个要刷新,然后还需要调用公共js文件,您需要将您的div id传递到上面的脚本中,而不是引用,我应该将脚本标记放在哪里?在div上面?在跳水之后?在这一页的顶部?那很奇怪。我把脚本放在文件的末尾,就在前面,在脚本中添加了src,在div中添加了id='quote',它不会刷新。。。