-1

I would like to track the download time of a specific file. So I am looking for are there any:

$(window).downloadComplete(function()
{....
});

If not, is it possible to track the download used time for a file through PHP/JavaScript?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user782104
  • 13,233
  • 55
  • 172
  • 312
  • No there is no such event, and anything is possible! – adeneo Mar 22 '13 at 08:14
  • Sorry for confusing , the download here does not mean finish the loading of the page, but finish loading of a file. eg. I go to nvidia.com to download a graphic card driver. I would like to get the download time of that setup.exe file – user782104 Mar 22 '13 at 08:21
  • If it's just to show an estimated download time on a page, you do that by dividing the filesize with a bandwidth, and then displays something like "takes 2 minutes to download with 10mbit DL speed" etc. – adeneo Mar 22 '13 at 08:25
  • Intresting question but its not possible with jQuery only, only if you let the file be downloaded via PHP, and update a file or mySQL record when the file is downloaded, and you got a longpolling request waiting on that update :) – EaterOfCode Mar 22 '13 at 08:26
  • 1
    @EaterOfCorpses - I've done that before with uploads, using cURL and writing the completed state to a file that was polled with an ajax request. Worked great, but not sure how you'd do that with a download, but it's definitively possible! – adeneo Mar 22 '13 at 08:29
  • @adeneo Sorry but in my case I am not to estimate the time for download, but I need to record the actual consumed sec / min /hour of downloading a specific file – user782104 Mar 22 '13 at 08:31
  • @adeneo only point I'm seeing now is you don't know the read speed of the user – EaterOfCode Mar 22 '13 at 08:31
  • 1
    @EaterOfCorpses - if you knew the connection speed of each user, it would be easy to just calculate an estimate based on file size, but as you don't know this, you'd have to somehow keep track. The solutions I'm aware of, using cURL or shell scripting only works for files downloaded to the server, or in other words uploaded by the user, not the other way around, but there probably are solutions for that as well, I just can't think of any right now ? – adeneo Mar 22 '13 at 08:38
  • found duplicate http://stackoverflow.com/questions/1563187/check-if-download-is-completed – EaterOfCode Mar 22 '13 at 10:00

2 Answers2

1

You can use the onload event to capture when something has been loaded (for example an image), or you could use an AJAX request with a success callback:

$.get('somefile.txt', function () {
    // executes when somefile.txt has been retrieved
});

Here is an onload example for an image, this doesn't even use jQuery:

var myImage = new Image();
myImage.onload = function () {
    // executes when the image is loaded
};
myImage.src = 'myimage.png';

If you are interested in the load time of the current page, you have two events you can use.

document ready (the HTML is all downloaded)

$(function () { 
    // the DOM is ready (so the HTML is downloaded, but images etc may not be)
});

onload (the document and all resources and images are downloaded)

window.onload = function () {
    // the DOM and all images etc are downloaded
};
Fenton
  • 241,084
  • 71
  • 387
  • 401
0

You mean on page loaded totally?
It's extractly:
$(document).onload(){
//do something
}