How can I Do Something after a variable is the same for X amount of time

I am a new Arduino hobbyist and I am interested in starting a project with an ultrasonic sensor. I need a

variable to be set to 1 after the sensor gives a readout of 10 for a time of 5 seconds.

What is your experience with software and hardware?

Read your sensor, if = 10 reset and enable a 5 second timer.
If not 10 disable timer.
If timer elapses, set variable.

Something like this pseudo code

myVar = getValueFromSensor();

if (myVar != 10) {
    lastTimeOutOfRange = millis();
}

if (millis() - lastTimeOutOfRange >= 5000) {
   // value has been correct for the time interval
   // so do something
}

...R