What function to use to set a timing for a function to stop

I'm using a L298N motor driver to drive a DC motor to move forward for 5 seconds and then move backwards for 5 seconds. I want it to move accordingly and set it to stop after let's say 30 seconds for example. What function do I use and is it possible to give an example on how to put in in the coding below.

int enA = 10;
int in1 = 9;
int in2 = 8;

void setup()
{
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
}

void demoOne()
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 200);
  delay(5000);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);   
  delay(5000);  
}

void loop()
{
  demoOne();
  delay(1000);
}

You need to look into using millis() for timing as in the BlinkWithoutDelay example and Several things at the same time

Save the time an event happens, such as motors starting, then each time through loop() check whether the required period has elapsed since the event. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action.

I want it to move accordingly and set it to stop after let's say 30 seconds for example

Use a for loop, in setup(), to make the motion each way, happen three times. Do nothing in loop().