简体   繁体   English

使用jQuery自动刷新div - setTimeout或其他方法?

[英]Auto-refreshing div with jQuery - setTimeout or another method?

How exactly do you make an auto-refreshing div with JavaScript (specifically, jQuery)? 你究竟用JavaScript(特别是jQuery)制作一个自动刷新div

I know about the setTimeout method, but is it really a good practice ? 我知道setTimeout方法,但它真的是一个好习惯吗? Is there a better method? 有更好的方法吗?

function update() {
    $.get("response.php", function(data) {
        $("#some_div").html(data);
    });
    window.setTimeout("update();", 10000);
}

Another modification: 另一个修改:

function update() {
  $.get("response.php", function(data) {
    $("#some_div").html(data);
    window.setTimeout(update, 10000);
  });
}

The difference with this is that it waits 10 seconds AFTER the ajax call is one. 与此不同的是,它在ajax调用为1之后等待10秒。 So really the time between refreshes is 10 seconds + length of ajax call. 所以刷新之间的时间实际上是10秒+ ajax调用的长度。 The benefit of this is if your server takes longer than 10 seconds to respond, you don't get two (and eventually, many) simultaneous AJAX calls happening. 这样做的好处是,如果您的服务器响应时间超过10秒,则不会发生两个(最终是很多)同时发生的AJAX调用。

Also, if the server fails to respond, it won't keep trying. 此外,如果服务器无法响应,它将不会继续尝试。

I've used a similar method in the past using .ajax to handle even more complex behaviour: 我过去使用类似的方法使用.ajax来处理更复杂的行为:

function update() {
  $("#notice_div").html('Loading..'); 
  $.ajax({
    type: 'GET',
    url: 'response.php',
    timeout: 2000,
    success: function(data) {
      $("#some_div").html(data);
      $("#notice_div").html(''); 
      window.setTimeout(update, 10000);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      $("#notice_div").html('Timeout contacting server..');
      window.setTimeout(update, 60000);
    }
}

This shows a loading message while loading (put an animated gif in there for typical "web 2.0" style). 这会在加载时显示加载消息(将动画gif放在那里以获得典型的“web 2.0”样式)。 If the server times out (in this case takes longer than 2s) or any other kind of error happens, it shows an error, and it waits for 60 seconds before contacting the server again. 如果服务器超时(在这种情况下需要超过2秒)或发生任何其他类型的错误,则会显示错误,并在再次联系服务器之前等待60秒。

This can be especially beneficial when doing fast updates with a larger number of users, where you don't want everyone to suddenly cripple a lagging server with requests that are all just timing out anyways. 当使用大量用户进行快速更新时,这可能特别有用,在这种情况下,您不希望每个人都突然瘫痪一个滞后的服务器,而这些服务的请求几乎都是超时的。

$(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() {
    $('#notice_div').load('response.php');
  }, 3000); // the "3000" 
});

你可能想尝试一个jQuery Timer插件

function update() {
  $("#notice_div").html('Loading..'); 
  $.ajax({
    type: 'GET',
    url: 'jbede.php',
    timeout: 2000,
    success: function(data) {
      $("#some_div").html(data);
      $("#notice_div").html(''); 
      window.setTimeout(update, 10000);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      $("#notice_div").html('Timeout contacting server..');
      window.setTimeout(update, 60000);
    }
});
}
$(document).ready(function() {
    update();
});

This is Better Code 这是更好的代码

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

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