Making a while-loop timeout

I understand that my while-loops are actually delaying my program while nyhet are locked in the loop.

Since the door only will be operrated twice a day and for å very short time each time (Max 10 sec), I did not think of that delay to be a problem for me. The fact that the microcontroller only is intended for the door and 1 temp sensor allso played in.

I am still in the beginner fase of programming, so I apriciate comments on how to gode smarter.

I understand that "howLongToWait" must be declared and sett to desired value earlier on.

Something like this?

void oppover()//---------------------------------Make door move up-----------------------------

    { 
      unsigned long startedWaiting = millis();
      while(digitalRead(topSwitchPin)==HIGH && millis() - startedWaiting <= howLongToWait) 
      {
        digitalWrite(motorOpp, HIGH);
        digitalWrite(motorNed, LOW);
        analogWrite(fart, 1023);
        Blynk.run();
        if (digitalRead(topSwitchPin)==HIGH && digitalRead(bottomSwitchPin)==HIGH) 
        {
          led.off();
        }
          if (digitalRead(topSwitchPin)==LOW)
        {
          stopp();
        }
      }
   }