简体   繁体   English

刷新div中的php脚本

[英]Refreshing php script inside a div

I am currently using the following script to refresh my messages.php in a div with a timeinterval of 60 seconds. 我目前正在使用以下脚本以60秒的时间间隔在div中刷新我的messages.php。

<script>
jQuery().ready(function(){
setInterval("getResult()",60000);
});
function getResult(){   
jQuery.post("messages.php",function( data ) {
    jQuery("#msgs").html(data);
});
}
</script>
<div id="msgs">
</div>

My current problem is that the echo of the php file displays for the first time after the 60 seconds passed. 我当前的问题是60秒过后首次显示php文件的回显。 Then it refreshes every 60 seconds. 然后每60秒刷新一次。 I need to get the code to load at the start of launching the page. 我需要在启动页面开始时加载代码。

Call getResult() on DOM Ready as well 在DOM Ready上也调用getResult()

<script>
jQuery().ready(function(){
getResult();
setInterval("getResult()",60000);
});
function getResult(){   
jQuery.post("messages.php",function( data ) {
    jQuery("#msgs").html(data);
});
}
</script>
<div id="msgs">
</div>

I would just use a setTimeout every time: 我每次都会使用setTimeout:

<script>
jQuery().ready(function() {
    function getResult() {
       $('#msgs').load('messages.php', {});
       setTimeout(getResult, 60000);
    }

    getResult();
}
</script>
<div id="msgs"></div>

Alternative with re-fire in the callback (suggestion of A. Wolff, in case messages.php takes long to complete): 在回调中重新触发的替代方法(建议A. Wolff,以防message.php需要很长时间才能完成):

<script>
jQuery().ready(function() {
    function getResult() {
       $('#msgs').load('messages.php', {}, function() {
           setTimeout(getResult, 60000);
       });
    }

    getResult();
}
</script>
<div id="msgs"></div>

In order to get data on page load, call getResult() on DOM ready event as shown below :- 为了在页面加载时获取数据,请在DOM ready事件上调用getResult() ,如下所示:

<script>
jQuery().ready(function(){
  getResult();  //it will get data as soon as page is loaded.
  setInterval("getResult()",60000); // it will refresh #msgs after every 1 min.
});
function getResult(){   
  jQuery.post("messages.php",function( data ) {
    jQuery("#msgs").html(data);
  });
}
</script>
<div id="msgs">
</div>
<script>
jQuery().ready(function(){
   getResult();
   setInterval("getResult()",60000);
});
function getResult(){   
jQuery.post("messages.php",function( data ) {
    jQuery("#msgs").html(data);
});
}
</script>
<div id="msgs">
</div>

Use this code.. 使用此代码。

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

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