Reputation: 199
I got a script witch puts urls from a txt file into an iframe, then checks the loading time of each source. If i have 4urls in my txt file, i get 3 results witch are good enough for me, but 1 result is always too short (says frame loaded in 0,016 seconds or so, where others take 1-4 seconds to load). If i delete other urls from my list, and only leave the one that gives bad value and then run my script - i get a normal value (few seconds to load).
Heres the script im talking about:
<script type="text/javascript">
$.get("imones.txt", function (data) {
var array = data.split(/\r\n|\r|\n/);
var beforeloadingtime = (new Date()).getTime();
var beforeTime = [];
beforeTime[0] = beforeloadingtime;
$.each(array, function (index, value) {
var jsonnuorodos = "http://anyorigin.com/get?url=" + array[index] + "&callback=?";
$.getJSON(jsonnuorodos, function (data) {
var iframe = $("#frame_id")[0];
var doc = iframe.document;
if (iframe.contentDocument) {
doc = iframe.contentDocument;
} else if (iframe.contentWindow) {
doc = iframe.contentWindow.document;
}
doc.open();
doc.writeln(data.contents);
doc.close();
var loadingtime = (new Date()).getTime();
beforeTime.push(loadingtime);
for (i = 0; i < array.length; i++) {
var result;
result = (beforeTime[i + 1] - beforeTime[i]) / 1000;
$("#loadingtimediv" + i).html(result);
}
});
});
});
</script>
Why do i get this inacurate result when running my script when i have multiple urls?
Edit: i think my script doesnt actually wait for 1 url to load completely before adding the next one, can this be the reason im getting false results?
Upvotes: 0
Views: 230
Reputation: 512
The result is probably accurate. Browsers typically download multiple resources at the same time. There's no reason to assume the files will be loaded serially, or even in the order you request them. (Imagine all the different files that need to be downloaded to display a web page: CSS, JS, images, frames...)
If you want to confirm, your browser probably has network debugging. For example, in the Chrome developer tools there is a network tab that will tell you the request and response times from the server.
Or try logging the difference between beforeloadingtime and each download completion time, instead of the difference between one download and the previous one.
Upvotes: 1