Timing on sensor issue

Hi,
My main sketch loop() has a delay(1) but I have a CO2 sensor that can't be read no faster than every 5 seconds, it'll error out otherwise. I can't change my delay to 5000 because that would make the whole app unresponsive.

I do have an RTC hooked up and I was thinking along the lines of

void loop() {
   getCO2();
   delay(1);
}

double getCO2() {
  if (RTC.second%5 == 0) {
    //read CO2 sensor
    return co2
  }
}

But the problem is if you call getCO2() at the wrong time, you're out of luck. I'd want to to return something on every call.
Or if I just use a while loop somehow without freezing up the app for 5 seconds?

thanks.

I can't change my delay to 5000 because that would make the whole app unresponsive.

Yes, it would. That's why there is the blink without delay example.

static unsigned long LastTimeCO2WasChecked = 0;

void loop () {
    if (millis() -LastTimeCO2WasChecked > 5000) {
        LastTimeCO2WasChecked = millis();
        double co2reading = getCO2();
        // act on co2 reading
    }
}

Thanks. I think those are the answers.