在PHP循环运行时加载动画 [英] Loading Animation while PHP loop runs

查看:57
本文介绍了在PHP循环运行时加载动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在php循环(for循环)运行时放一个加载动画可以说这是一个有10个数字的for循环.我想将动画显示为1 loading ..当秒数开始时,它显示2仅加载(而不同时显示1和2)希望您能正确理解我的问题.能否为您提供帮助

I want to put a loading animation while php loop (for loop ) runs Lets say it's a for loop with 10 numbers . I want to show an animation as 1 Loading.. when the seconds number starts it shows 2 Loading only (not both 1 & 2) I hope you can understand my question properly .Can you please help me on this

 <html>
<head>
<style>
.loading:after {
  content: ' .';
  animation: dots 1s steps(5, end) infinite;}

@keyframes dots {
  0%, 20% {
    color: rgba(0,0,0,0);
    text-shadow:
      .25em 0 0 rgba(0,0,0,0),
      .5em 0 0 rgba(0,0,0,0);}
  40% {
    color: white;
    text-shadow:
      .25em 0 0 rgba(0,0,0,0),
      .5em 0 0 rgba(0,0,0,0);}
  60% {
    text-shadow:
      .25em 0 0 white,
      .5em 0 0 rgba(0,0,0,0);}
  80%, 100% {
    text-shadow:
      .25em 0 0 white,
      .5em 0 0 white;}}


</style>
<script type="text/javascript">

</script>
</head>
<body>               
<?php
$i=0;
for ($i=0;$i <= 10; $i++)
{
  echo "  <p class='loading'>$i Loading</p>";
}
?>
  </body>
</html>

推荐答案

按照您的设置方式,您想要的东西是不可能的.那是因为PHP的工作原理.

The way you have it set up, what you want is not possible. That's because of how PHP works.

当PHP嵌入到这样的HTML文件中时,它将在将页面发送给用户之前在服务器上运行.时间线如下:

When PHP is embedded in an HTML file like that, it's run on the server before the page is sent to the user. The timeline goes like this:

  1. 用户请求页面
  2. 服务器找到该页面的文件
  3. 服务器在每个块中运行代码,并将结果替换为php标记.
  4. 服务器将生成的HTML文档发送给用户

如您所见,当用户看到任何内容时,所有php代码都已经完成.

As you can see, by the time the user sees anything, all the php code has already finished.

要在网页中获得任何形式的动态行为(如加载栏),您必须使用JavaScript(或编译成JS之类的TypeScript).这是因为JavaScript代码与HTML一起发送给用户,并在页面加载后由用户的浏览器 执行.

To get any kind of dynamic behavior in your webpage (like a loading bar) you HAVE to use JavaScript (or something that compiles to JS like TypeScript). That's because JavaScript code is sent to the user along with the HTML and executed by the user's browser after the page loads.

大概是因为要运行一些PHP代码,所以需要显示进度表.您仍然可以这样做.除了将PHP嵌入HTML之外,您还可以制作一个独立的PHP文件来执行所需的任务,然后使页面上的JavaScript向该服务器发送第二个请求,并在显示其进度的同时获得结果.您要搜索的术语是"PHP REST服务器"(用于服务器)和"JavaScript XHR"或"JavaScript获取",用于从客户端发送请求.

Presumably, you want to show a progress meter because you wanted to run some PHP code that would take a while. You can still do that. Instead of embedding PHP inside HTML, you can make a stand-alone PHP file that does the task you want and then have the JavaScript on your page send a second request to that server and get a result while showing its progress. The terms you want to google are "PHP REST server" (for the server) and "JavaScript XHR" or "JavaScript fetch" for sending requests from the client.

这篇关于在PHP循环运行时加载动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆