Spinner直到PHP解析somesing

时间:2016-06-08 16:18:15

标签: javascript php

在我的PHP中,我正在解析Json链接,我正在搜索脚本,因为脚本会显示加载器直到解析完成。 例如:

<?php

     echo " spinner here" ; 

     sleep(5);//lets say this is parsing json script

?>

我尝试了很多东西而且没有用,请帮忙。

2 个答案:

答案 0 :(得分:1)

如果你想用Ajax做,这就是它的工作原理:HTML代码有一个按钮,当点击它启动“spinner”并调用PHP解析代码时,当PHP解析代码完成时,它返回到Ajax和spinner停止了:

<强> spinner.html

<html>
  <head>
    <script src='https://code.jquery.com/jquery-2.1.3.min.js'></script>
    <script type="text/javascript">
function myAjax ()
{ $("#my_div").html( "Wait while parsing..." );      // START SPINNER.
  $.ajax( { type    : 'POST',
            url     : 'spinner.php',                 // CALL PHP CODE.
            success : function ( result )
                      { $("#my_div").html( result ); // STOP SPINNER.
                      },
            error   : function ( xhr )
                      { alert( "error" );
                      }
          }
        );
}
    </script>
  </head>
  <body>
    <button onclick="myAjax()">Parse JSON links</button>
    <div id="my_div"></div>                         <!-- YOUR SPINNER HERE -->
  </body>
</html>

<强> spinner.php

<?php
sleep( 5 );
echo "Parsing finished!";
?>

只需创建两个具有给定名称的文件,复制粘贴这些代码并运行!

答案 1 :(得分:0)

如果您只想在php中执行此操作,请查看flushob_flush

我不得不连续两次打电话给他们才能让它发挥作用。

<?php

echo "spinner here..." ; 

flush();
ob_flush();
flush();
ob_flush();

sleep(5);//lets say this is parsing json script

echo "done!";