首页 > 解决方案 > 仅使用 setinterval 更新 div

问题描述

我正在尝试使用 setinterval 每隔一定时间更新一个表,如果表被更新,问题是整个页面都被更新并且加倍,这是一个示例代码:

<!DOCTYPE HTML>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>

</head>

<body>
  <h1>hello</h1>

  <div id="updategreeting">
    <h2>hello2</h2>
  </div>

</body>

</html>

<script type="text/javascript">
  $(document).ready(function() {

    setInterval(

      function() {
        $("#updategreeting").empty().load("#updategreeting");
      }, 10000

    );
  });
</script>

在导航器中执行时会显示(hello hello2),但是当以设置的间隔充电时会显示(hello hello hello2

然后如果我只想更新 div(updategreeting)而不加载(hello),我希望你能帮助我,谢谢。

标签: javascriptjquerysetinterval

解决方案


推荐阅读