"Multitasking" not working correctly

Hey,
I have got a question about the "Multitasking" with an arduino.
I have a Board with some Leds which can be toggled on and off with switches.
If toggled on the LEDs blink in an interval.

I realized it with the millis() function and a time that gets updated every time the function is circled.
The Problem is, when i haven't pushed the button for a longer time the LEDs blink very fast.
How can that problem be solved?, I think it is because the timer that checks if a blinking cycle is finished didn't get updated for a long time.

Here is the Code Example:

//Blinking 6 Leds
if(Button2State == 1){
if(millis() > time_led6 + period_led6){
  for(int i=2; i<8; i++){
 leds6 = random(0,2);

 if(leds6 == 0){
   datArray[0] &= ~(1 << i);
 }
 else datArray[0] |= (1 << i);
time_led6 += period_led6;
}
}
}else{
   for(int i=2; i<8; i++){
     datArray[0] &= ~(1 << i);
   }
}

First, subtract time_led6 from both sides so it's comparing durations rather than absolute timestamps:

to make:

if(millis() -time_led6 >  period_led6){

That eliminates the dreaded millis() rollover problem.

Then your repeated flashes is caused by this line which makes sure you have an event for every period that has passed, whether or not the button was pressed:

To skip over the time that the button wasn't pressed, try:

time_led6 = millis();

couldn't resist.
It's working :).