Skip to content

Commit 131acc7

Browse files
added all files
0 parents  commit 131acc7

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

js/jquery.speedtest.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
(function($) {
3+
$.fn._speedTest = function(options) {
4+
5+
function foo(){
6+
var stest = $.extend({
7+
fileSize: null,
8+
fileType: null,
9+
fileUrl: null,
10+
waitingText: null,
11+
errorText: null
12+
13+
}, options),
14+
duration, download, startTime, endTime, bitsLoaded,cacheBuster,speedMbps,speedKbps,speedBps,p;
15+
16+
17+
if (stest.fileType == "text") {
18+
download = new Document();
19+
} else if (stest.fileType == "image") {
20+
download = new Image();
21+
}
22+
23+
if (stest.errorText) {
24+
download.onerror = function(err, msg) {
25+
$(this).text(stest.errorText);
26+
};
27+
} //error if
28+
29+
startTime = (new Date()).getTime();
30+
cacheBuster = "?spdt=" + startTime;
31+
download.src = stest.fileUrl + cacheBuster;
32+
p = function see(){
33+
endTime = (new Date()).getTime();
34+
duration = (endTime - startTime) / 1000;
35+
bitsLoaded = stest.fileSize * 8;
36+
speedBps = (bitsLoaded / duration).toFixed(2);
37+
console.log(speedBps);
38+
speedKbps = (speedBps / 1024).toFixed(2);
39+
console.log(speedKbps);
40+
speedMbps = (speedKbps / 1024).toFixed(2);
41+
console.log(speedMbps);
42+
return speedMbps;
43+
};
44+
return p;
45+
}// end of foo
46+
47+
48+
return this.each(function() {
49+
var g = foo();
50+
$(this).text(g);
51+
}); //end of for each function
52+
53+
}; //end of the main function
54+
}(jQuery));

speedTest.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>Speed test plugin</title>
8+
9+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
10+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/octicons/3.1.0/octicons.min.css">
11+
12+
<!--[if lt IE 9]>
13+
<script src="https://cdn.jsdelivr.net/html5shiv/3.7.2/html5shiv.min.js"></script>
14+
<script src="https://cdn.jsdelivr.net/respond/1.4.2/respond.min.js"></script>
15+
<![endif]-->
16+
</head>
17+
<body>
18+
<h2 id="t">should display here if no error </h2>
19+
<p></p>
20+
<script src="https://cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js"></script>
21+
<script src="https://cdn.jsdelivr.net/bootstrap/3.3.5/js/bootstrap.min.js"></script>
22+
<script src="js/jquery.speedtest.js"></script>
23+
<script>
24+
$('p')._speedTest({
25+
fileSize:12800,
26+
fileType:"image",
27+
fileUrl:"http://mobinius.com/wp-content/uploads/2012/02/silicon-India.png"
28+
});
29+
</script>
30+
</body>
31+
</html>

0 commit comments

Comments
 (0)