When the browser registers your CSS declaration it registers the name of the animation background1 and the duration of 10s and then starts the animation immediately. If you want to restart the animation with a new duration you must wait till the animation finishes, or you must attach a different animation name with the new duration.
You can test at this fiddle: http://jsfiddle.net/mfdj/Phnf5/9/
- You can always immediately trigger an animation when you change the name
- You can restart the same animation by changing just the duration only if you set a different value for the duration and do so after the the current animation has finished
Since your initial animation is 10s long and you want to immediately trigger a duration with a randomized time you should simply remove this declaration:
#background1 {
-webkit-animation: background1 10s;
}
and you should get the result you want.
Please note that there are some quirks in the way browsers handle reattaching/retriggering css animations with the same name but different durations. For instance, in the example fiddle Chrome handles the shake and flash animations differently. Try starting a 10second animation and then interrupt with a 1second animation of the same name. The shake will finish it's 10s while the flash simply clears the animation. However, if you switch from flash to shake (and vice versa) the animation always starts fresh. These are the quirks of triggering animation with css that you must be aware of.