Why would we need 10s delay after connect to the internet

From this example: https://www.arduino.cc/en/Tutorial/Wifi101WiFiChatServer

Right after connecting to the internet, the code delay for 10 seconds before continuing. Does anyone know why we would need this?

The code is a bit confusing. The delay is within a loop that runs until connected, intended to wait some time before a retry. Remember that it may be interpreted as a DOS attack if a robot tries to connect in quick sequence, not leaving the network a chance to process all preceding attempts.

You can rewrite that part like this:

while (status != WL_CONNECTED) {
  ...
  status = WiFi.begin(...);
  if (status != WL_CONNECTED) delay(10000);
}

DrDiettrich:
The code is a bit confusing. The delay is within a loop that runs until connected, intended to wait some time before a retry. Remember that it may be interpreted as a DOS attack if a robot tries to connect in quick sequence, not leaving the network a chance to process all preceding attempts.

You can rewrite that part like this:

while (status != WL_CONNECTED) {

...
  status = WiFi.begin(...);
  if (status != WL_CONNECTED) delay(10000);
}

this is even more confusing

Looks like poor coding to me.

  // attempt to connect to WiFi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

The loop tries to connect to a WiFi access point, and continues trying to do so forever with a 10 second break between attempts.

That’s very shoddy code - it will loop forever if the WiFi connection cannot be established, e.g. if the Access Point is not reachable.

My guess is that who ever wrote that code never bothered to test it when the access point (WiFi-AP) was switched off.

I’d be looking to attempt a maximum of three attempts.

int connect_wifi()
{
  // attempt 3 connects
  int attempt =0;

  // attempt to connect to WiFi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    if(attempt ++ >= 3)break;

    // wait 1 second to try again:
    delay(1000);
  }
return status;
}