Php:如何在延迟一秒后打印每个结果

时间:2016-12-23 13:58:04

标签: php loops for-loop

我想要类似的东西:

for($k=0;$k<20;$k++){
    echo $k;
}

输出:

0

睡眠1秒钟。

1

睡1秒钟。

2

睡1秒钟。

5 个答案:

答案 0 :(得分:0)

如果您对逐个显示在页面上的变量感兴趣,并且两者之间有第二次延迟,那么您需要使用JavaScript而不是PHP。如果您需要PHP的输入,请使用Ajax。

PHP有一个名为sleep()http://php.net/manual/en/function.sleep.php)的内置函数,它会导致代码延迟给定的秒数。但是,这将具有您可能期望的行为,也就是说,回显变量,然后等待一秒,然后回显另一个变量。该脚本将花费更长的时间来执行。

所以,例如:

for($k=0;$k<20;$k++){
    echo $k;
    sleep(1);
}

执行需要20秒,但页面仍然只会加载一次。

答案 1 :(得分:0)

我认为您正在寻找输出缓冲区:

for ($k = 0; $k < 20; $k++) {
    echo $k . '<br />';
    flush();
    ob_flush();
    sleep(1);
}

答案 2 :(得分:-1)

使用内置睡眠功能 //以秒为单位的时间

for($k=0;$k<20;$k++){
    echo $k;
    sleep(1);
}

答案 3 :(得分:-1)

来自http://docs.php.net/Thread 使用 pthreads

$strRemove = "This is a small tést to convert $'s in to €'s!"
$Wrapper = $null
$Wrapper = @{
    'é' = 'e'
    '€' = 'Euro'
    '$' = 'Dollar'
}
$Wrapper.Keys | foreach-object{
    $strRemove = $strRemove.replace("$_" , $Wrapper[$_])
}
$strRemove

答案 4 :(得分:-1)

如果您打算在CLI中执行此操作,那么您需要使用sleep,就像这样

public class CenterZoomLayoutManager extends LinearLayoutManager {

    private final float mShrinkAmount = 0.15f;
    private final float mShrinkDistance = 0.9f;

    public CenterZoomLayoutManager(Context context) {
        super(context);
    }

    public CenterZoomLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }


    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        int orientation = getOrientation();
        if (orientation == VERTICAL) {
            int scrolled = super.scrollVerticallyBy(dy, recycler, state);
            float midpoint = getHeight() / 2.f;
            float d0 = 0.f;
            float d1 = mShrinkDistance * midpoint;
            float s0 = 1.f;
            float s1 = 1.f - mShrinkAmount;
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                float childMidpoint =
                        (getDecoratedBottom(child) + getDecoratedTop(child)) / 2.f;
                float d = Math.min(d1, Math.abs(midpoint - childMidpoint));
                float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);
                child.setScaleX(scale);
                child.setScaleY(scale);
            }
            return scrolled;
        } else {
            return 0;
        }
    }

    @Override
    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
        int orientation = getOrientation();
        if (orientation == HORIZONTAL) {
            int scrolled = super.scrollHorizontallyBy(dx, recycler, state);

            float midpoint = getWidth() / 2.f;
            float d0 = 0.f;
            float d1 = mShrinkDistance * midpoint;
            float s0 = 1.f;
            float s1 = 1.f - mShrinkAmount;
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                float childMidpoint =
                        (getDecoratedRight(child) + getDecoratedLeft(child)) / 2.f;
                float d = Math.min(d1, Math.abs(midpoint - childMidpoint));
                float scale = s0 + (s1 - s0) * (d - d0) / (d1 - d0);
                child.setScaleX(scale);
                child.setScaleY(scale);
            }
            return scrolled;
        } else {
            return 0;
        }

    }
}

但是,如果您打算在Web浏览器上执行此操作,那么PHP会在服务器响应客户端之前在服务器上运行,因此您的所有20秒都花在服务器上,浏览器将在以下位置接收所有输出一旦。因此,对于浏览器,您的方法是不够的,而不是那样,您可能想要进行轮询,或setTimeout或setInterval。由于您对PHP循环感兴趣,我建议您可以从命令行运行PHP。

相关问题