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
.
Here is the code:
<script type="text/javascript">
$.get("imones.txt", function (data) {
var array = data.split(/
||
/);
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. Instead of reading this imones.txt
file, i want to replace it with an php array:
$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:
$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;
}