Skip to content

Commit 6f7845c

Browse files
authored
add load and error function support
do not change text, user can change text in the function.
1 parent e469b8f commit 6f7845c

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

js/jquery.speedtest.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
21
(function($) {
32
// A simple speedtest plugin to check download speed in a network.
43
//Author : Harsha Jagadish
5-
//Version : V 0.1
4+
//Version : V 0.2
65
// TODO: Add waiting message and waiting image
76
$.fn._speedTest = function(options) {
87

@@ -31,15 +30,15 @@
3130
} //error if
3231

3332
startTime = (new Date()).getTime();
34-
console.log(startTime);
33+
console.log("startTime:" + startTime);
3534
cacheBuster = "?spdt=" + startTime;
3635
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
3938
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);
4342
bitsLoaded = stest.fileSize * 8;
4443
speedBps = (bitsLoaded / duration).toFixed(2);
4544
console.log(speedBps);
@@ -49,6 +48,17 @@
4948
console.log(speedMbps);
5049
return speedMbps;
5150
};
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+
});
5262
return p;
5363
}// end of foo
5464

@@ -57,7 +67,8 @@
5767
//TODO: remove too many variable names
5868
var g = foo();
5969
// 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");
6172
}); //end of for each function
6273

6374
}; //end of the main function

0 commit comments

Comments
 (0)