使用ajax更新Django模板数据。

问题描述 投票:0回答:1

我正在尝试使用ajax脚本在Django模板上定期更新数据(10秒)。我对前端开发比较陌生。

使用其他文章,我是可以做到的。但是每次页面刷新的时候,都会创建多个线程进行页面刷新,而且每10秒的更新请求都会增加一倍。

以下是我的django模板片段。

<body id="page-top">
 <div class="table-responsive">
  <table class="table table-striped">
    <thead>
      <tr class="table-info">
        <th style="text-align:center">Parameter</th>
        <th style="text-align:center">A</th>
        <th style="text-align:center">B</th>
      </tr>
    </thead>
      <tbody>
      {% for para, details in manualData.items %}
      <tr>
         <th style="text-align:center" scope="row">{{ details.1.name }}</th>
           {% for key, entity in details.items %}
        <td style="text-align:center">
                <font color="white" size="4px">{{ entity.value }}</font>
            </td>
           {% endfor %}
          </tr>     
      {% endfor %}
      </tbody>
  </table>
 </div>
</body>

我使用的是如下的ajax脚本。

<script type="text/javascript">
        function refresh() {
         var_ref =  $.ajax({
                success: function (data) {
                    $('#page-top').html(data);
                }
            });
        }
        $(function () {
            setInterval('refresh()', 10000);
        });
    </script>

最后,我需要的是:

一旦刷新过程结束 叫做, 新工艺 应运而生,否则 如果要定义新的进程,过去的进程将被中止。.

请帮助我实现同样的目标。

先谢谢你

Nakul

django ajax django-templates
1个回答
0
投票

你需要你的ajax调用是同步的,而不是异步的,这样你就会阻塞线程,直到你得到你需要的数据。

可以通过添加 在ajax调用中的相应属性

在行业内,你通常会使用模板系统来加载像react、angular或vue这样的东西,它们会不断地更新DOM,而不必进行无休止的轮询。

© www.soinside.com 2019 - 2024. All rights reserved.