Extra frames using DrawDots

Hi, The below is my code.
I would like to draw white dots for 60 frames and black dots for 120 frames (this is intertrial intervals) on the black screen. I could get exact number of 60 frames for the first white dots periods but getting extra frames for the black dots. I cannot troubleshoot it. Please advise.

Screen('Preference', 'SkipSyncTests',2);
PsychDefaultSetup(2);
scrsize = [0 0 1920 1080];
[win,rect] = Screen('OpenWindow',0, 0);
Screen('BlendFunction', win, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
flipt= Screen('Flip', win);
Screen('FillRect', win,  [1 1 1], [1450 1050 1500 1080])

for ii = 1:24
dotwhite=[255 255 255]*1; dotblack=[1 1 1]*1;
dotsarray = createdots (2000, coherence (ii), 450, 7.5, direction (ii), 25);  %2.5 for 1s; 7.3 for 3s; 1300 frames for 1s; 3300 frames for 3s
flipt= Screen('Flip', win);
    for frameIdx = 1:dotsarray{2} 
        if  frameIdx <= 60    **%% draw white dots**
         Screen('DrawDots', win, dotsarray{1}(:, :, frameIdx)',  dotsarray{3}, dotwhite, [1920 1080]*.5, 2);
         Screen('FillRect', win,  [255 255 255], [1450 1050 1500 1080])
         Screen('Flip', win, flipt+((1/60)*.7)); 
 
        else  **%% draw black dots**
         Screen('DrawDots', win, dotsarray{1}(:, :, frameIdx)',  dotsarray{3}, dotblack, [1920 1080]*.5, 2);
         Screen('FillRect', win,  [1 1 1], [1450 1050 1500 1080])
         Screen('Flip', win, flipt+((1/60)*.7)); 
        end
 
    end

you will have the author of createdots how that function works, its not ours. The comment "%2.5 for 1s; 7.3 for 3s; 1300 frames for 1s; 3300 frames for 3s" appears nonsensical
Also, flipt is used wrongly, it should be refreshed on every iteration: flipt = Screen(‘Flip’, win, flipt+((1/60).7));
That doesn’t fix your problem, but should be done.

Screen(‘Preference’, ‘SkipSyncTests’,2); will make sure any timing is shot, so durations could be very wrong

This means you should then use 0 to specify black and 1 to specify white, yet you don’t use this for the dots…

This is using the frame number for timing. But if you drop frames then your timing becomes wrong. Especially using 'SkipSyncTests',2 you cannot really know if your timing is accurate (well, you do seem to use a photodiode but this requires extra effort later)…