简体   繁体   English

需要帮助修改javascript(将php值插入其中)

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

Got this javascript that reads data (urls) from a txt file, then puts each url in a frame, calculates how much time it took to load that url in iframe, then display the result into html div . 得到了这个从txt文件读取数据(网址)的javascript,然后将每个网址放入一个框架中,计算将该网址加载到iframe中所花费的时间,然后将结果显示到html div

Here is the code: 这是代码:

<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 has data written in it like so - example1.com, example2.com and so on. imones.txt像这样写有数据-example1.com example1.com, example2.com等。 Instead of reading this imones.txt file, i want to replace it with an php array: 与其阅读此imones.txt文件, imones.txt将其替换为php数组:

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

And then instead of displaying the result into html div ( $("#loadingtime" + [index]).html(result); ) i want to put that result into another php array: 然后,而不是将结果显示到html div( $("#loadingtime" + [index]).html(result); )中,我想将该结果放入另一个php数组中:

$php_array[] = $("#loadingtime" + [index]).html(result);

Can someone help me do this? 有人可以帮我吗?

Work over this idea: 解决这个想法:

// 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;
}

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

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