Try this example that will spawn a random cube everytime the frame == 10, it will stop after 5 cubes:
import bpy
# this is for the random position, imports the random() function from random library
from random import random
# this holds how many cubes were created and what the cap is
spawned_cubes = 0
max_cubes = 5
def spawn_object(scene):
"""This function will be run everytime before frame changes because it is added in the callback list"""
global spawned_cubes
global max_cubes
# read the current frame number
# if it is 10 and number of spawned cubes is less than max_cubes place a cube
# if the frame number is not 10 just end and do nothing
if scene.frame_current == 10 and spawned_cubes < max_cubes:
loc = (5-10*random(), 5-10*random(), 5-10*random())
bpy.ops.mesh.primitive_cube_add(enter_editmode=False, location=loc)
spawned_cubes += 1
return
# add spawn_object function if there isnt one in bpy.app.handlers.frame_change_pre callback list
# the function needs to be added there only once
# everything thats in that list will be executed before any frame change
if spawn_object not in bpy.app.handlers.frame_change_pre:
bpy.app.handlers.frame_change_pre.append(spawn_object)
This uses the app handlers callback list. In your spawn_object function you can test any property - for example the location of your cube_object, or some frame value etc.