简体   繁体   English

Javascript函数仅返回1个值而不是值数组

[英]Javascript function returning just 1 value instead of an array of values

I am trying to find out page loading time by using iframes. 我试图通过使用iframe找出页面加载时间。 I have an array of urls, that i put into an array of iframes, and then i get the loadtime of each iframe by using javascript. 我有一个网址数组,将其放入iframe数组中,然后使用javascript获取每个iframe的加载时间。 My problem - my time values is not an array, so it just shows 1 value (it shows value of 1st frame then second later it replaces it with value of 2nd frame on top of it). 我的问题-我的时间值不是一个数组,所以它只显示1个值(它显示第一个帧的值,然后显示第二个帧的值,第二个之后将其替换为它的顶部)。

   <html>
    <body>   
    <center>
    <?php 
    $url = array('example.com', 'example2.com');
    foreach($url as $i){ 
    ?>
    <iframe onload="pageloadingtime();" width="505" height="505" scrolling="no" security="restricted" src="<?php echo "http://www.".$i; ?> "></iframe>
    <?php
    } 
    ?>
    <div id="loadingtime"></div>
    </center>
    </body>
    <script type="text/javascript">    
    beforeload = (new Date()).getTime();
    function pageloadingtime()
    {
        afterload = (new Date()).getTime();
        secondes = (afterload-beforeload)/1000;
        document.getElementById("loadingtime").innerHTML = "<font color='red'>(Page took " + secondes + " seconds to load)</font>";
    }   
    </script>
    </html>

In this code i make an array of urls ($url), run a foreach cycle making as many iframes as there are urls, and then using javascript i count the page loading time. 在这段代码中,我制作了一个网址数组($ url),运行一个foreach周期,制作与该网址一样多的iframe,然后使用javascript计算页面加载时间。 After i get the loading time i put it into the "loadingtime" div. 得到加载时间后,我将其放入“ loadingtime” div。 But like i said it only puts 1 value instead of putting an array of values. 但是就像我说的那样,它只放置1个值,而不是放置一个值数组。

changed it to this, but now its not displaying anything: 更改为此,但现在不显示任何内容:

beforeload = (new Date()).getTime();
function pageloadingtime()
{
    var afterload = (new Date()).getTime();
    secondes = (afterload-beforeload)/1000;
    document.getElementById("loadingtime1").innerHTML += "<font color='red'>(Page took " + secondes + " seconds to load)</font><br>";
}

Append to the status div instead of overwriting it: 附加到状态div而不是覆盖它:

document.getElementById("loadingtime").innerHTML += "<font color='red'>(Page took " + secondes + " seconds to load)</font><br>";

Also, declare afterLoad and secondes as local to the function: 另外,将afterLoadsecondes声明为该函数的局部变量:

var afterload = (new Date()).getTime();
var secondes = (afterload-beforeload)/1000;

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

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