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

标签 php javascript jquery

得到这个 javascript,它从 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 中写入的数据如下 - example1.com、example2.com 等等。我不想读取这个 imones.txt 文件,而是想用 php 数组替换它:

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

然后我不想将结果显示到 html div ($("#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;
}

关于php - 需要修改 javascript 的帮助(将 php 值插入其中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15548641/

相关文章:

php - 如何正确链接php-fpm和Nginx Docker容器?

javascript - 如何通过javascript将CSRF token 添加到动态生成的表单中

javascript - 实现了 reCaptcha...仍然收到垃圾邮件

javascript - 根据部分隐藏和显示信息

php - 获取另一个 PHP 文件的内容并使用当前 PHP 文件的变量

php - 使用php显示oracle数据库中的多行

php - 属性存在但 property_exists() 返回 false;

javascript - TwentyTwenty 隐藏后点击显示没有高度

javascript - 链接颜色变化

javascript - .on ('click' , function(){}) 仅在第一次点击时起作用