【问题标题】:How to display ajax's function result in endless loop?如何在无限循环中显示ajax的函数结果?
【发布时间】:2020-06-24 17:16:52
【问题描述】:

这是我第一次使用 Ajax,我使用无限循环 ajax 的请求从 python 函数中获取数据。 我想在 html 表中显示数据,但是当 ajax 循环时,屏幕正在加载并且什么都没有显示。 这是我的 ajax 代码。

  <td height="50%"style="border-width:5px; border-color:black; border-style: solid">
    <p id = "demo">hihi</p>
      <script>
      while(True){
        $(document).ready(function calllog(){
          $.ajax({
            type : 'GET',
            url : '/for_log',
            dataType : 'text',
            error : function(){
              alert("failed");
            },
            success : function(data){
              document.getElementById("demo").append(data);
              }
            }
          });
        }
        });

如何在不继续加载屏幕的情况下,一边接收和写入数据一边打印屏幕?

【问题讨论】:

    标签: javascript html ajax flask


    【解决方案1】:

    你应该避免while(true)。最好使用setInterval

    setInterval(()=>{
            $(document).ready(function calllog(){
              $.ajax({
                type : 'GET',
                url : '/for_log',
                dataType : 'text',
                error : function(){
                  alert("failed");
                },
                success : function(data){
                  document.getElementById("demo").append(data);
                  }
                }
              });
            }
            } , 1000); // every second
    

    这是解决此问题的另一种方法recursive ajax

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-22
      • 2014-12-23
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多