Delay for rewriting 15 lines to TFT

I have a function that’s called by my void loop(). It reads saved alerts from a file on the SD card and displays the lines by 15 of them on the TFT at a time. After hitting 15 lines, it erases the previous 15 and displays the next 15. The function is working as intended at the moment, but the problem is the fact that I need the erasing and displaying part of my function to be delayed. I also need this delay to not delay the whole program. I tried millis() and it doesn’t seem to work because I need the delay place after if(counter == 15). The function code is posted below, any help would be appreciated.

void viewRecentAlerts(){
  if(currentLayer == 8){
    
    int cursorY = 0;
    int numLines = 0;
    int counter = 0;
       
    if(currentPress == "Main Menu"){
      resetSelect();
      clearMenu();
      mainMenu();
    }
    
  //open the file for reading
  alertFile = SD.open(fileName);
  if(alertFile){
        while(alertFile.available()){
        String temp = (alertFile.readStringUntil('\n'));
        numLines++;
    }
    alertFile.close();
  }
  Serial.println("Number of Lines:");
  Serial.println(numLines);
  int displayLength = (numLines / 15) + 1;
  Serial.println("Round:");
  Serial.println(displayLength);

  //re-open the file for reading
  alertFile = SD.open(fileName);
  if(alertFile){
    //read from the file until there's nothing else in it
    for(int i = 0; i <= numLines; i++){
      tft.setTextColor(TFT_WHITE,TFT_BLACK);
      cursorY = cursorY + 30;
      tft.setCursor(15,cursorY);
      tft.println(alertFile.readStringUntil('\n'));
      counter++;
      if(counter == 15){
          cursorY = 0;
          counter = 0;
        }
      }
    }
    //close the file
    alertFile.close();
  }
 }

Write the 15 lines and save the value of millis() as lastDisplayTime then each time through loop() check whether the current time - lastDisplayTime equals 15 seconds. If not then keep going round loop() until it does. Once 15 seconds has passed clear the screen, write 15 lines, save lastDisplayTime and repeat until all pages have been displayed.

Note that no other code in loop() must block the free running of the program

Do you mean delay the entire function? If that’s what you meant, it won’t work in my case. I specifically need the below section of my function to be delayed. Particularly right after if(counter == 15). Including the millis() delay, exactly like you just said, inside this part of the code causes the for loop to not display the correct lines within the file.

If I do the below, it delays and prints just fine. However, it delays the entire program.

    for(int i = 0; i <= numLines; i++){
      tft.setTextColor(TFT_WHITE,TFT_BLACK);
      cursorY = cursorY + 30;
      tft.setCursor(15,cursorY);
      tft.println(alertFile.readStringUntil('\n'));
      counter++;
      if(counter == 15){
          delay(5000);
          cursorY = 0;
          counter = 0;
        }
      }

Try splitting your function up. Create one that just displays 15 lines. You can either keep the file open and just do the next fifteen or pass a parameter that tells the function which line to start at.

Call this new function from loop and control when it's called using millis.

Do you mean delay the entire function?

No. The problem is the for loop.

Call the function each time through loop() if you are currently displaying lines (use a boolean to control this). Do away with the for loop and count the total lines shown so far as you print each of them

Thank you wildbill and UKHeliBob for the advice! It's working great now.