Skip to content

Commit

Permalink
Use unique names for nested callbacks (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
SCG82 authored Feb 9, 2021
1 parent 011f884 commit 53fb62e
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions speedtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,17 @@ Speedtest.prototype = {
throw "You can't select a server while the test is running";
}
if (this._selectServerCalled) throw "selectServer already called"; else this._selectServerCalled=true;
/*this function goes through a list of servers. For each server, the ping is measured, then the server with the function result is called with the best server, or null if all the servers were down.
/*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.
*/
var select = function(serverList, result) {
var select = function(serverList, selected) {
//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.
var PING_TIMEOUT = 2000;
var USE_PING_TIMEOUT = true; //will be disabled on unsupported browsers
if (/MSIE.(\d+\.\d+)/i.test(navigator.userAgent)) {
//IE11 doesn't support XHR timeout
USE_PING_TIMEOUT = false;
}
var ping = function(url, result) {
var ping = function(url, rtt) {
url += (url.match(/\?/) ? "&" : "?") + "cors=true";
var xhr = new XMLHttpRequest();
var t = new Date().getTime();
Expand All @@ -217,11 +217,11 @@ Speedtest.prototype = {
if (d <= 0) d = p.duration;
if (d > 0 && d < instspd) instspd = d;
} catch (e) {}
result(instspd);
} else result(-1);
rtt(instspd);
} else rtt(-1);
}.bind(this);
xhr.onerror = function() {
result(-1);
rtt(-1);
}.bind(this);
xhr.open("GET", url);
if (USE_PING_TIMEOUT) {
Expand Down Expand Up @@ -271,7 +271,7 @@ Speedtest.prototype = {
)
bestServer = serverList[i];
}
result(bestServer);
selected(bestServer);
}.bind(this);
var nextServer = function() {
if (i == serverList.length) {
Expand Down

0 comments on commit 53fb62e

Please sign in to comment.