需要帮助修改javascript(插入一个php值到它)


Need help modifying javascript (inserting a php value into it)

从一个txt文件中读取数据(url),然后将每个url放在一个框架中,计算在iframe中加载该url所需的时间,然后将结果显示在html div中。

代码如下:

<script type="text/javascript">
   $.get("imones.txt", function (data) {
        var array = data.split(/'r'n|'r|'n/);
        var beforeLoad = (new Date()).getTime(); 
        var loadTimes = []; 
            var beforeTimes = [];               
        $('#frame_id').on('load', function () {                                 
            beforeTimes.push(beforeLoad);
            loadTimes.push((new Date()).getTime()); 
            $('#frame_id').attr('src', array.shift());
                try {
                $.each(loadTimes, function (index, value) { 
                    var result = (value - beforeTimes[index]) / 1000; 
                        if (result < 0) { 
                            result = result * (-1);
                        }
                    $("#loadingtime" + [index]).html(result);                       
                    beforeLoad = value; 
                });
                } catch(ex) {}
        }).attr('src', array.shift()); 
    });
</script>

imones.txt中写入的数据如so - example1.com, example2.com等。而不是阅读这个imones.txt文件,我想用php数组代替它:

$url_array = array();
$url_array[] = 'example1.com';
$url_array[] = 'example2.com';

然后,而不是显示结果到htmldiv ($("#loadingtime" + [index]).html(result);)我想把结果到另一个php数组:

$php_array[] = $("#loadingtime" + [index]).html(result);
谁能帮我做这件事?

仔细考虑这个想法:

// frontend
$.get(
    "path_to_get_url_array", // server function
    function(url_array){ // this receive your $url_array data
        $.each(url_array, function(i, item) {
            // run your code over item to calculate time
            // save loadingtime
            var loadingtime = '';
            $.post(
                    "path_to_save_loadingtime", // server function
                    {
                        loadingtime = $("#loadingtime" + [index]).html(result);
                    }
            });
    }, 'json');
// backend get_url_array
function get_url_array()
{
    // get $url_array from somewhere
    echo json_encode($url_array);
    exit;
}
// backend save_loadingtime
function save_loadingtime()
{
    $loadingtime = $_REQUEST['loadingtime'];
    // save $loadingtime to db
}
// when you want acess you data get it 
function create_array()
{
    // read all $loadingtime values from db     
    // populate $php_array
    return $php_array;
}