Refresh content in a DIV

在一个div刷新内容

问题 (Question)

So I know how to refresh content that is being loaded in to a div from another page, but is there anyway to refresh content that's just sitting (already in on the same page) in a div?

Example

<?php $this = 1; ?>

<div id="refresh_this"> 
    <?php echo $this; ?> 
</div>

所以我知道如何更新内容被加载到另一页一个div,但反正是要刷新的内容,只是坐在(已在同一页)在一个div?

的例子

<?php $this = 1; ?>

<div id="refresh_this"> 
    <?php echo $this; ?> 
</div>

最佳答案 (Best Answer)

You would have to use ajax to load that content from the server again. You should look into push technology using websockets. Check out Ratchet for a php web socket, or do some research in web sockets in general.

A simpler approach would be polling, but it is kind of rough and not really as well rounded as web sockets. Polling is what came before sockets, and sockets are definitely the way to go.

There are two basic types of polling. Long polling and short polling.

Long polling is going to involve hitting the server and then the server fires a background thread which waits until it gets results, then returns the results at some time, which is when the poll fires again, and the server waits etc.

Short polling is where you hit the server once every x milliseconds, and then the server instantly responds and you deal with the response.

你将不得不使用Ajax加载,内容从服务器再次。你应该考虑使用WebSockets推送技术。检查棘轮一个PHP Web Socket,或在一般的Web Sockets做一些研究。

一个简单的方法,将投票,但它是一种粗糙和不作为全面的网络插座。投票是在插座,插座是一定要走的路。

有两种基本类型的投票。长轮询和短轮询。

长轮询是要打击服务器,然后服务器触发一个背景线程等待直到它得到的结果,然后返回一些结果,这是当调查火灾再次,和服务器等等。

短的投票是你打服务器每隔x毫秒,然后服务器立即响应,你处理的响应。

答案 (Answer) 2

I'm sure this is a duplicate question but here goes:

$("#refresh_this").html('<?php echo $this; ?>');

我相信这是一个重复的问题,但这里:

$("#refresh_this").html('<?php echo $this; ?>');

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/16925993