简体   繁体   English

先前的刷新完成后,Javascript刷新php页面

[英]Javascript refresh php page when previous refresh has finished

I am using this code to refresh a PHP page: 我正在使用以下代码刷新PHP页面:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
setInterval(function() {
$('.container').load('integra_data.php');
}, 2000); // the "3000" here refers to the time to refresh the div. it is in milliseconds.
});
// ]]></script>

<div class="container"><h3>Loading Data...</h3></div>

in javascript, rather than refreshing every X Seconds, can i make it refresh once the previous refresh has finished? 在JavaScript中,而不是每隔X秒刷新一次,是否可以在上一次刷新完成后使其刷新?

使用setTimeout代替setInterval。

$(document).ready(function() {
  $.ajaxSetup({ cache: false }); 
  setTimeout(function() {
    $('.container').load('integra_data.php');
  }, 2000); 
});

$( document ).ready(function(){}) everything inside of this will be ran after load of page has finished. $( document ).ready(function(){})所有内容将在页面加载完成后运行。 so there should be delay of 2 seconds here.. 所以这里应该有2秒的延迟。

If I got you right you want to refresh the content of the div in an infinite loop, right? 如果我理解正确,您想无限循环刷新div的内容,对吗? If this is right, you could pass your call to load() as complete callback. 如果这是正确的,则可以将调用作为complete回调传递给load() You could do somethin like this (recursion): 您可以执行以下操作(递归):

$(document).ready(function() {
  var loadFunction = function () {
    $('.container').load('integra_data.php', loadFunction);
  }
  $.ajaxSetup({ cache: false }); 
  loadFunction();
});

The loadFunction() will call itself every time the last run has finished. 每当最后一次运行结束时, loadFunction()都会调用自身。 Refer to the docs if you want to know more about the complete callback. 如果您想了解有关complete回调的更多信息,请参考文档 If you add a little delay, since it's not always the best idea to make request as fast as possible use the setTimeout() function. 如果增加一点延迟,因为使用setTimeout()函数尽可能快地发出请求并非总是最好的主意。 You could add it to the complete callback. 您可以将其添加到complete回调中。

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

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