简体   繁体   English

显示实时值

[英]Display Real-Time Values

Say I have the following: 说我有以下几点:

for ($i = 0; $i < 10; $i++) {
    echo $i;
    sleep(1);
}

Now, this will display: 现在,将显示:

0123456789

However, it will take 9 seconds to load, and will not display real-time. 但是,加载需要9秒钟,并且不会实时显示。 How would I display it so it would be: 我将如何显示它,它将是:

0(1 second)2(1 second)3(...)

My second question involves overwriting the current data on the page. 我的第二个问题涉及覆盖页面上的当前数据。 For example, say I have the same code as above. 例如,假设我具有与上面相同的代码。 However, I want to display each number as itself. 但是,我想显示每个数字本身。 So the page would be: 因此页面将是:

0

Then after 1 second 然后1秒后

1

And so on. 等等。

for ($i = 0; $i < 10; $i++) {
    echo $i."\n";
    $timeFirst  = strtotime(date());
    sleep(1);
    $timeSecond = strtotime(date());
    echo ($timeSecond - $timeFirst)." second(s) \n";
}

You can only do it with Client side script such as javascript. 您只能使用javascript之类的客户端脚本来执行此操作。 DOM loads only after the complete execution of your server side script. 仅在完全执行服务器端脚本之后才加载DOM

There is no way you can execute each command and render the output to the web browser, unless you are doing it at CLI or doing it via Client Side. 您无法执行每个命令并将输出呈现到Web浏览器,除非您在CLI上进行操作或通过客户端进行操作。

Try this 尝试这个

for ($i = 0; $i < 10; $i++) {
    echo $i;
    flush();
    ob_end_flush();
    sleep(1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM