Change ElementArrayStim color with certain frequency

Hello,

I have two ElementArrayStim stimuli: stimA and stimB.
I would like the window to alternate between stimA and stimB at a set frequency, that I can ideally hook into to execute code when the drawing is done.

What is the most precise and efficient way of accomplishing this?

The Code

# Create the stimuli
stimA = visual.ElementArrayStim(win, xys=xys, fieldPos=loc, colors=colorsA, ...
stimA.size = ...

stimB = visual.ElementArrayStim(win, xys=xys, fieldPos=loc, colors=colorsB, ...
stimB.size = ...

# Don't quit till user presses a key
while not event.getKeys():
    # Draw the checkerboard
        stimA.draw()
        win.flip()

For example, this will alternate the stimuli on each frame (i.e. on a 60 Hz display, each stimulus would be shown flickering at 30 times per second):

while not event.getKeys():
    # Draw the checkerboard
    stimA.draw()
    win.flip()

    stimB.draw()
    win.flip()

You would need to specify your timing requirements to get an answer more specific to your needs.