Skip to content

Commit 2b53bba

Browse files
authored
Changed var to the let/const because it's completely supported by IE11 and all other browsers since 2015. (#656)
1 parent c9fdd2f commit 2b53bba

File tree

2 files changed

+104
-104
lines changed

2 files changed

+104
-104
lines changed

speedtest.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
The best way to learn how to use this is to look at the basic example, but here's some documentation.
1313
1414
To initialize the test, create a new Speedtest object:
15-
var s=new Speedtest();
15+
let s=new Speedtest();
1616
Now you can think of this as a finite state machine. These are the states (use getState() to see them):
1717
- 0: here you can change the speed test settings (such as test duration) with the setParameter("parameter",value) method. From here you can either start the test using start() (goes to state 3) or you can add multiple test points using addTestPoint(server) or addTestPoints(serverList) (goes to state 1). Additionally, this is the perfect moment to set up callbacks for the onupdate(data) and onend(aborted) events.
1818
- 1: here you can add test points. You only need to do this if you want to use multiple test points.
@@ -125,7 +125,7 @@ Speedtest.prototype = {
125125
* Same as addTestPoint, but you can pass an array of servers
126126
*/
127127
addTestPoints: function(list) {
128-
for (var i = 0; i < list.length; i++) this.addTestPoint(list[i]);
128+
for (let i = 0; i < list.length; i++) this.addTestPoint(list[i]);
129129
},
130130
/**
131131
* Load a JSON server list from URL (multiple points of test)
@@ -144,11 +144,11 @@ Speedtest.prototype = {
144144
if (this._state == 0) this._state = 1;
145145
if (this._state != 1) throw "You can't add a server after server selection";
146146
this._settings.mpot = true;
147-
var xhr = new XMLHttpRequest();
147+
let xhr = new XMLHttpRequest();
148148
xhr.onload = function(){
149149
try{
150-
var servers=JSON.parse(xhr.responseText);
151-
for(var i=0;i<servers.length;i++){
150+
const servers=JSON.parse(xhr.responseText);
151+
for(let i=0;i<servers.length;i++){
152152
this._checkServerDefinition(servers[i]);
153153
}
154154
this.addTestPoints(servers);
@@ -193,27 +193,27 @@ Speedtest.prototype = {
193193
if (this._selectServerCalled) throw "selectServer already called"; else this._selectServerCalled=true;
194194
/*this function goes through a list of servers. For each server, the ping is measured, then the server with the function selected is called with the best server, or null if all the servers were down.
195195
*/
196-
var select = function(serverList, selected) {
196+
const select = function(serverList, selected) {
197197
//pings the specified URL, then calls the function result. Result will receive a parameter which is either the time it took to ping the URL, or -1 if something went wrong.
198-
var PING_TIMEOUT = 2000;
199-
var USE_PING_TIMEOUT = true; //will be disabled on unsupported browsers
198+
const PING_TIMEOUT = 2000;
199+
let USE_PING_TIMEOUT = true; //will be disabled on unsupported browsers
200200
if (/MSIE.(\d+\.\d+)/i.test(navigator.userAgent)) {
201201
//IE11 doesn't support XHR timeout
202202
USE_PING_TIMEOUT = false;
203203
}
204-
var ping = function(url, rtt) {
204+
const ping = function(url, rtt) {
205205
url += (url.match(/\?/) ? "&" : "?") + "cors=true";
206-
var xhr = new XMLHttpRequest();
207-
var t = new Date().getTime();
206+
let xhr = new XMLHttpRequest();
207+
let t = new Date().getTime();
208208
xhr.onload = function() {
209209
if (xhr.responseText.length == 0) {
210210
//we expect an empty response
211-
var instspd = new Date().getTime() - t; //rough timing estimate
211+
let instspd = new Date().getTime() - t; //rough timing estimate
212212
try {
213213
//try to get more accurate timing using performance API
214-
var p = performance.getEntriesByName(url);
214+
let p = performance.getEntriesByName(url);
215215
p = p[p.length - 1];
216-
var d = p.responseStart - p.requestStart;
216+
let d = p.responseStart - p.requestStart;
217217
if (d <= 0) d = p.duration;
218218
if (d > 0 && d < instspd) instspd = d;
219219
} catch (e) {}
@@ -234,14 +234,14 @@ Speedtest.prototype = {
234234
}.bind(this);
235235

236236
//this function repeatedly pings a server to get a good estimate of the ping. When it's done, it calls the done function without parameters. At the end of the execution, the server will have a new parameter called pingT, which is either the best ping we got from the server or -1 if something went wrong.
237-
var PINGS = 3, //up to 3 pings are performed, unless the server is down...
237+
const PINGS = 3, //up to 3 pings are performed, unless the server is down...
238238
SLOW_THRESHOLD = 500; //...or one of the pings is above this threshold
239-
var checkServer = function(server, done) {
240-
var i = 0;
239+
const checkServer = function(server, done) {
240+
let i = 0;
241241
server.pingT = -1;
242242
if (server.server.indexOf(location.protocol) == -1) done();
243243
else {
244-
var nextPing = function() {
244+
const nextPing = function() {
245245
if (i++ == PINGS) {
246246
done();
247247
return;
@@ -261,10 +261,10 @@ Speedtest.prototype = {
261261
}
262262
}.bind(this);
263263
//check servers in list, one by one
264-
var i = 0;
265-
var done = function() {
266-
var bestServer = null;
267-
for (var i = 0; i < serverList.length; i++) {
264+
let i = 0;
265+
const done = function() {
266+
let bestServer = null;
267+
for (let i = 0; i < serverList.length; i++) {
268268
if (
269269
serverList[i].pingT != -1 &&
270270
(bestServer == null || serverList[i].pingT < bestServer.pingT)
@@ -273,7 +273,7 @@ Speedtest.prototype = {
273273
}
274274
selected(bestServer);
275275
}.bind(this);
276-
var nextServer = function() {
276+
const nextServer = function() {
277277
if (i == serverList.length) {
278278
done();
279279
return;
@@ -284,17 +284,17 @@ Speedtest.prototype = {
284284
}.bind(this);
285285

286286
//parallel server selection
287-
var CONCURRENCY = 6;
288-
var serverLists = [];
289-
for (var i = 0; i < CONCURRENCY; i++) {
287+
const CONCURRENCY = 6;
288+
let serverLists = [];
289+
for (let i = 0; i < CONCURRENCY; i++) {
290290
serverLists[i] = [];
291291
}
292-
for (var i = 0; i < this._serverList.length; i++) {
292+
for (let i = 0; i < this._serverList.length; i++) {
293293
serverLists[i % CONCURRENCY].push(this._serverList[i]);
294294
}
295-
var completed = 0;
296-
var bestServer = null;
297-
for (var i = 0; i < CONCURRENCY; i++) {
295+
let completed = 0;
296+
let bestServer = null;
297+
for (let i = 0; i < CONCURRENCY; i++) {
298298
select(
299299
serverLists[i],
300300
function(server) {
@@ -323,7 +323,7 @@ Speedtest.prototype = {
323323
this.worker.onmessage = function(e) {
324324
if (e.data === this._prevData) return;
325325
else this._prevData = e.data;
326-
var data = JSON.parse(e.data);
326+
const data = JSON.parse(e.data);
327327
try {
328328
if (this.onupdate) this.onupdate(data);
329329
} catch (e) {

0 commit comments

Comments
 (0)