您的位置:首页 > Web前端

Yahoo的Flush the Buffer Early 思想!

2010-05-06 18:43 337 查看
When users request a page, it can take anywhere from 200 to 500ms for the backend server to stitch together the HTML page. During this time, the browser is idle as it waits for the data to arrive. In PHP you have the function flush(). It allows you to send your partially ready HTML response to the browser so that the browser can start fetching components while your backend is busy with the rest of the HTML page. The benefit is mainly seen on busy backends or light frontends.

A good place to consider flushing is right after the HEAD because the HTML for the head is usually easier to produce and it allows you to include any CSS and JavaScript files for the browser to start fetching in parallel while the backend is still processing.

Example:

... <!-- css, js -->
</head>
<?php flush(); ?>
<body>
... <!-- content -->

PHP官方的flush()说明

是将截止目前,程序的所有输出 打印出去。 这样,PHP下面的代码即便没有执行完毕,也会输出一部分代码。

例如

网页要即时计算一些数据,打印给网民。

我们先将HTML的<header>打印出去,然后再打印boay的内容。这样就提高了一定量的速度哦~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: