As long as connected is equal to true, the while loop will keep firing over and over, with no delay in between. A while loop is not asynchronous - it will pause everything else going on in the script until it is over.
Think of a loop like an automatic code writer. It writes a function that goes for as long as until the statement in the brackets is false.
while (connected == true) {
testConnection();
}
is equivalent of writing:
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
if (connected == true) {
testConnection();
}
...
over and over again for as long as connected is equal to true. So if connected stays equal to true, the while loop tries to write an infinitely long code, causing an infinite loop until the testConnection() function makes connected equal to false. If your computer had infinite memory, this infinitely long code would take up all of it - but your computer doesn't have an infinite supply of memory, and so it crashes.