Javascript 有意减慢 HTML/PHP 页面加载以进行测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7405987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:09:22  来源:igfitidea点击:

Intentionally Slow Down HTML/PHP Page Load to Test

phpjavascriptjquerytestingload-testing

提问by stefmikhail

I'm curious if there exists a method to intentionally slow the page load?

我很好奇是否有一种方法可以故意减慢页面加载速度?

I'm testing my HTML & PHP pages on my local host right now, and I want to see how my loading gif etc will perform when the page load is slower.

我现在正在本地主机上测试我的 HTML 和 PHP 页面,我想看看当页面加载速度较慢时我的加载 gif 等将如何执行。

I realize this is a rare request as most developers are only concerned with speeding uppage loads, but I thought there might be a method, using either javascript/jQuery or PHP, to do something like this for testing purposes.

我意识到这是一个难得的请求,因为大多数开发者只关注加快页面加载,但我认为有可能是一个方法,即使用的JavaScript / jQuery的或PHP,做这样的事情出于测试目的。

Thanks for any help!

谢谢你的帮助!

Note: I'm testing on MAMP, so that's Apache server running on Mac OS 10.7

注意:我正在 MAMP 上进行测试,因此这是在 Mac OS 10.7 上运行的 Apache 服务器

采纳答案by Cyclone

You can use php's sleep($seconds)function to slow down a page load. However, you would need to turn implicit output buffer flushing to "on"with ob_implicit_flush(true);if you want anything to be sent to the user's browser before the page is done being processed. Otherwise your page won't have ANY contents until it's done loading. Calling sleep alone won't do the trick.

您可以使用 php 的sleep($seconds)功能来减慢页面加载速度。但是,你需要把隐含的输出缓冲区刷新到“开”ob_implicit_flush(true);你想要的网页做任何事情之前被发送到用户的浏览器正在处理中。否则你的页面在加载完成之前不会有任何内容。单独调用 sleep 不会成功。

回答by Bolphgolph

In Chrome, you can simulate a slow Internet connection with the developer tools. under the "Network" Tab on the far right. You can use a preset like "Fast G3" or can create your own with exact numbers for upload, download and ping.

在 Chrome 中,您可以使用开发人员工具模拟慢速 Internet 连接。在最右侧的“网络”选项卡下。您可以使用“Fast G3”之类的预设,也可以使用准确的数字创建自己的预设,以进行上传、下载和 ping。

enter image description here

在此处输入图片说明

Reference: https://helpdeskgeek.com/networking/simulate-slow-internet-connection-testing/

参考:https: //helpdeskgeek.com/networking/simulate-slow-internet-connection-testing/

回答by Maximus

Moussahas the right idea. The best way to test a slow page load is to use Chrome's developer tools. Select the network tab, and then click the dropdown that says "No throttling". Then change the page to your desired speed.

穆萨的想法是正确的。测试缓慢页面加载的最佳方法是使用 Chrome 的开发人员工具。选择网络选项卡,然后单击显示“无节流”的下拉菜单。然后将页面更改为您想要的速度。

This way is superior to using the sleep function because you don't have to mess with any code and then remember to take it out. If you want to change the speed, just change the throttling level and you're set.

这种方式比使用sleep函数要好,因为你不用乱写任何代码,然后记得把它拿出来。如果您想更改速度,只需更改节流级别即可。

For more information on throttling check out the docs.

有关节流的更多信息,请查看文档

回答by xtrem

This is what I would try: Use a php resource as source of the image:

这就是我要尝试的:使用 php 资源作为图像的来源:

<img src="images/gifLoager.php" />

in gifLoader.php, read your image file, output it byte by byte with a delay in the loop.

在 gifLoader.php 中,读取您的图像文件,在循环中延迟逐字节输出。

$fp = fopen( $path, 'rb');
while(!feof($fp)) {
        print(fread($fp, 1024));
        flush();
        Sleep(1);
     }
fclose($fp);

Don't forget to appropriately set the headers before outputting the binary data.

不要忘记在输出二进制数据之前适当地设置标题。

Referrences:

参考资料:

http://stackoverflow.com/questions/1563069/stream-binary-file-from-mysql-to-download-with-php
http://php.net/manual/en/function.sleep.php
http://www.gamedev.net/topic/427622-php-and-file-streaming-script-with-resume-capability/

UPDATE 2015-04-09Use Chrome 'Device Mode': This tool has a network throttling feature that allows you to see how your page may render on a device with a slow network bandwidth. It has many other features that allow you to emulate features on various devices such as screen size and touch.

更新 2015-04-09使用 Chrome 的“设备模式”:此工具具有网络节流功能,可让您查看页面在网络带宽较慢的设备上的呈现方式。它还有许多其他功能,可让您模拟各种设备上的功能,例如屏幕尺寸和触摸。

https://developer.chrome.com/devtools/docs/device-mode

https://developer.chrome.com/devtools/docs/device-mode

回答by Joel A. Villarreal Bertoldi

You can use sleep():

您可以使用sleep()


<?php
// Delays for 10 seconds.
sleep(10);
?>

...
html here
...

回答by Eric

Call sleep()in your PHP code to delay the request to the server.

调用sleep()您的 PHP 代码以延迟对服​​务器的请求。

回答by Joe

A little bit of JS setTimeout can do the trick

一点点 JS setTimeout 就可以解决问题

setTimeout(function()
{
    // Delayed code in here
    alert('You waited 5 seconds to see me'); // Waits 5 seconds, then alerts this
}, 5000); // 5000 = 5 seconds

回答by Matija Nalis

you can use sloppy local web proxyto slow down your connection (it's in JAVA, so it'll probably run on your devel machine. You might also want to turn off mod_deflate for testing purposes, if you want so how your browser responds to slow HTML load (for example, dynamicly sized HTML tables, etc)

您可以使用草率的本地 Web 代理来减慢您的连接速度(它在 JAVA 中,因此它可能会在您的开发机器上运行。您可能还想关闭 mod_deflate 以进行测试,如果您愿意,您的浏览器如何响应缓慢HTML 加载(例如,动态大小的 HTML 表格等)

Also, see this questionon webmasters.stackexchange.com

另外,请参阅webmasters.stackexchange.com 上的此问题