|
1 |
| - |
2 | 1 | (function($) {
|
3 | 2 | // A simple speedtest plugin to check download speed in a network.
|
4 | 3 | //Author : Harsha Jagadish
|
5 |
| - //Version : V 0.1 |
| 4 | + //Version : V 0.2 |
6 | 5 | // TODO: Add waiting message and waiting image
|
7 | 6 | $.fn._speedTest = function(options) {
|
8 | 7 |
|
|
31 | 30 | } //error if
|
32 | 31 |
|
33 | 32 | startTime = (new Date()).getTime();
|
34 |
| - console.log(startTime); |
| 33 | + console.log("startTime:" + startTime); |
35 | 34 | cacheBuster = "?spdt=" + startTime;
|
36 | 35 | download.src = stest.fileUrl + cacheBuster;
|
37 |
| - p = function see(){ |
38 |
| - // TODO: change the calculation to handle the error managment |
| 36 | + p = function see(){ |
| 37 | + // TODO: change the calculation to handle the error management |
39 | 38 | endTime = (new Date()).getTime();
|
40 |
| - console.log(endTime); |
41 |
| - duration = (endTime - startTime) / 1000; |
42 |
| - console.log(duration); |
| 39 | + console.log("endTime:" + endTime); |
| 40 | + duration = Math.max((endTime - startTime), 1) / 1000; |
| 41 | + console.log("duration:"+duration); |
43 | 42 | bitsLoaded = stest.fileSize * 8;
|
44 | 43 | speedBps = (bitsLoaded / duration).toFixed(2);
|
45 | 44 | console.log(speedBps);
|
|
49 | 48 | console.log(speedMbps);
|
50 | 49 | return speedMbps;
|
51 | 50 | };
|
| 51 | + $(download).on('load', function callback(){ |
| 52 | + p(); |
| 53 | + if(stest.onLoad){ |
| 54 | + stest.onLoad.call(this, duration, speedMbps) |
| 55 | + } |
| 56 | + }).on('error',function(err, msg) { |
| 57 | + p(); |
| 58 | + if(stest.onError){ |
| 59 | + stest.onError.call(this, duration, speedMbps, event) |
| 60 | + } |
| 61 | + }); |
52 | 62 | return p;
|
53 | 63 | }// end of foo
|
54 | 64 |
|
|
57 | 67 | //TODO: remove too many variable names
|
58 | 68 | var g = foo();
|
59 | 69 | // TODO: make the speed extension as a variable to fit user needs
|
60 |
| - $(this).text(g).append(" Mbps"); |
| 70 | + // $(this).text(g).append(" Mbps"); |
| 71 | + // $(this).text(g).append("Mbps"); |
61 | 72 | }); //end of for each function
|
62 | 73 |
|
63 | 74 | }; //end of the main function
|
|
0 commit comments