Let's suppose that you want to detect a change of state from HIGH to LOW
Each time the HIGH to LOW change occurs save the value of millis() and set a boolean to true. Any time that the input is LOW set the boolean to false.
Each time through loop() if the boolean is true check whether 5 seconds has elapsed since timing started. If so then set the boolean to false to prevent further triggering and run any other required code
void loop() {
buttonState = read button
if (buttonState == wrong) {
mostRecentWrongStateTime = millis()
}
if (millis() - mostRecentWrongStateTime > desiredInterval) {
// button has been in the correct state throughout the interval
}
}