解释以下(2)计时功能/脚本之间的区别

时间:2013-12-17 04:28:17

标签: javascript php performance load page-load-time

为什么PHP会在Javascript之前完成并返回结果加载时间?

因此PHP的返回速度比Javascript快得多,我相信这对大多数人来说都是一个快速的。让我知道。

<?php
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
?>

<!doctype html>
<html>
<head>
<script>
    var d = new Date();
    var starttime = d.getTime();
</script>
</head>
<body>
<p id="loadtime">Write your custom message here</p>
<script>
    for(i=0; i<1000000000;i++){}
</script>
<script>
    var d2 = new Date();
    var endtime = d2.getTime();
    var totaltime = (endtime - starttime)/1000;
    var result = totaltime;
    document.getElementById("loadtime").innerHTML = "Page loaded in: " + result + " seconds";
</script>
</body>
</html>
<?php
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 8);
echo 'Page generated in '.$total_time.' seconds.';
?>

1 个答案:

答案 0 :(得分:4)

因为PHP不执行javascript,所以浏览器会执行。

PHP代码在服务器中执行,它准备javascript并简单地将其发送到浏览器。它是浏览器执行javascript的工作。