如何刷新< DIV> [英] How to refresh <DIV>

查看:104
本文介绍了如何刷新< DIV>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何刷新div里面的自动刷新div是php



以下代码不工作?

  var refresh = setInterval(function(){$(#recent_activity)。html();},1); 


解决方案

基于x0n的评论,我认为这是一个公平的猜测,我猜你想要每隔X秒自动刷新一次来自服务器的内容< div> 。在你的例子中,你对 setInterval 函数的第二个参数是1.这是一个非常糟糕的主意,因为延迟以毫秒为单位,所以你会发出1000个请求第二(不是浏览器会让你,但仍然)。

无论如何,如果这是你想要的,你需要这样做:

  var timer; 
var seconds = 30; //我们应该多久刷新DIV?

function startActivityRefresh(){
timer = setInterval(function(){
$('#recent_activity')。load('recent_activity_ajax.php');
},seconds * 1000)
}

函数cancelActivityRefresh(){
clearInterval(timer);你的服务器端脚本( recent_activity_ajax.php >

/ code>)然后需要返回任何你想要的< div> 填充 - 只是,不是页眉或页脚或还要别的吗。如果您希望在页面加载时启动,您可以简单地调用 startActivityRefresh

  $(function(){
startActivityRefresh();
});

如果这完全不是您要刷新< div> ; ,你需要澄清。 :)

编辑



回应你的评论:让Javascript刷新动态PHP代码的内容。这是不可能的,因为浏览器无法真正执行像PHP这样的服务器端语言。您必须再次呼叫服务器。为了使这个最干净,你应该把填充 #recent_activity 内容的代码移动到一个函数中,在页面加载时调用一次,并有一个简单的文件输出代码以便能够动态刷新它。你应该从这里看看MVC模式,但我不确定你是否已经准备好了......


How to refresh the auto refresh div inside the div theres is php

The following code dont work?

var refresh = setInterval(function() {  $("#recent_activity").html(); }, 1);

解决方案

Based on x0n's comment, which I think is a fair guess, I am guessing that you want to automatically refresh a <div> with content from the server every X seconds. In your example, your second argument to the setInterval function is 1. This is a really bad idea, as the delay is in milliseconds, so you would be firing off 1000 requests a second (not that the browser would let you, but still).

Anyhow, if that's what you want, you would need to do this:

var timer;
var seconds = 30; // how often should we refresh the DIV?

function startActivityRefresh() {
    timer = setInterval(function() {
        $('#recent_activity').load('recent_activity_ajax.php');
    }, seconds*1000)
}

function cancelActivityRefresh() {
    clearInterval(timer);
}

Your server-side script (recent_activity_ajax.php in the example) would then need to return whatever you want the <div> to be populated with - just that, not the headers or footers or anything else. If you wanted this to start when the page loads, you would simply call startActivityRefresh:

$(function() {
    startActivityRefresh();
});

If this is not at all what you meant by refreshing the <div>, you're going to need to clarify. :)

EDIT:

In response to your comment: you can't make Javascript "refresh" the contents of the dynamic PHP code. This just isn't possible as the browser can't really execute a server side language like PHP. You have to call the server again. In order to make this cleanest you should probably move the code that fills the contents of #recent_activity to a function, call that once when the page loads and have a different file that simply outputs that code to be able to refresh it dynamically. You should probably look into MVC patterns from here, but I'm not sure if you're quite ready for that...

这篇关于如何刷新&lt; DIV&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆