calling a timer when a button is pushed
When your code:

if (currentMillis - previousMillis &gt;= onTime){
    digitalWrite(ledPin2, LOW);
    previousMillis = millis();
}


was in the loop, it was executed multiple times, so when if (currentMillis - previousMillis &gt;= onTime) eventually became true, it would set ledPin2 LOW again.
Now that you moved it to the timer funtion, it only gets executed once (and if (currentMillis - previousMillis &gt;= onTime) never gets a chance to become true).

You need to introduce a loop in the timer function. It also looks like you have your currentMillis / previousMillis messed up. I think you are looking for something like this:

void timer(){

    unsigned long previousMillis = millis();
    unsigned long currentMillis = millis();
    digitalWrite(ledPin2, HIGH);

    while((currentMillis - previousMillis) &lt; onTime) {
        // wait for a bit
        sleep(10);
        // update currentMillis
        currentMillis = millis();
    }

    digitalWrite(ledPin2, LOW);
}