Before we start, you need to have basic idea of what AJAX is, if you need to you can read my Introduction to AJAX post. Also, this time instead of creating all functions by myself i used advAJAX, it is lite (only 15kB) and very easy to use AJAX framework, created by Polish web developer who goes by the name Anakin. You can download it here. Full english documentation can be found here.
Well actually, there is also fourth step, we need to calculate connection speed and there are few things that you should keep in mind when doing this:
- difference between time2 and time1 will be in miliseconds, so you have to multiply it by 1000 to get number of seconds needed to download file
- common connection speed unit is kbps (kilo bits per second), in our example we will use kBps which stands for kilo bytes per second. If you do not know what is the difference between them checkout wikipedia for more information.
- we are not really measuring real connection speed because, final result is affected by many other factors, but it still will be a pretty good estimation
Here is full source code. after clicking check button function is executed and whole process of testing connection speed starts, it goes like i described earlier: read timestmp, download file, read timestamp, calculate and display results. You maybe also wondering why i used advAjax instead of some known AJAX library, and the reason for this is that advAJAX has built in custom event: onProgress, which passes three arguments to the function; obj: AJAX object, intReceived: number of bytes downloaded, intTotal: total number of bytes to download, nice, right? Unfortunately onProgress event works only with FireFox and Opera.
In last tutorial i promised full source code for download to every tutorial, however this time full source code is already on screen, just download advAjax, copy and paste code above to new file, create data.txt and fill it with random content. Remember to keep all files in the same directory and … have fun with it