Stopping a function after a period of time

Hello,

Is it possible to stop a function after a period of time? The program I made, sometimes takes more than one second and sometimes less. I would like to stop de function when it takes one second or more to finish the function. The code I wrote is very big, so I simplified the code:

void setup() {
  unsigned long time = millis();
  Serial.begin(9600);
  while (millis() - time < 1000) {
    Time();
  }
}

void Time() {
  delay(1500);
  Serial.println("Check");
}

void loop() {
  // put your main code here, to run repeatedly:
}

I tried this, but I think as long as millis() - time < 1000 the code starts the function Time(). Is it possible to start Time() once and to stop Time() as soon it takes more than one second to finish?

Thank you in advance.

Is it possible to start Time() once and to stop Time() as soon it takes more than one second to finish?

Not until you get rid of that blocking delay()

As written, the Time() function will always take 1500 milliseconds to execute. You need to either use millis() for timing in the function and exit the function when the required period has ended or better still restructure the code so that Time() is called frequently until the period has passed. This would, of course, require the code in the function to be written so that it can be called again and again.

See Several things at the same time

NO. there is no part of the language that would just stop a function in the middle of execution.

you would have to manually code checkpoints inside the function that would cause it to return or skip.

void myfunction (){
  unsigned long check = millis();

  // time consuming calculation here
  if(millis()-check>1000){return;}
  // more time consuming calculation here
  if(millis()-check>1000){return;}
  //yet even  more time consuming calculations
  if(millis()-check>1000){return;}
  // blah blah blah 
  }

Rather extreme but...if the function's inability to complete it's operation in some pre-determined time period is considered a "critical" error you could use a timer and an interrupt. The interrupt could call a function that is mapped to 0 and software reset the board. It won't guarantee reset of all of the hardware though and may not work on all boards.

void(* resetFunc) (void) = 0; //declare reset function @ address 0
.
.
.
void resetIsr(void)
{
  resetFunc();  //call reset
}