Repeat a Chain Sequentially for a Set Amount of Time
Chains can be repeated sequentially for a certain length of time by the placement of two scripts within the chain itself.
Any number of Messages or other Clip Elements can be placed between the two Scripts.
- The first script, Record start time, is placed at the beginning of the chain and records the starting time of the first repeat of the chain in a Chain Custom Property.
//Is this the first repeat of this chain?
//This is the first repeat of this chain.
//Get the Chain's property list
var propList = $context.currentChain.propertyList;
//Create the StartTime property, ignoring any errors
//if the list already exists.
//Set the property value to the time of this, the first
//repeat. (in milliseconds since January 1, 1970
propList.setPropertyValue("Start Time", Date.now())
- The second script, Check elapsed time, is placed at the end of the chain and measures the elapsed time at the end of each repeat. If the desired amount of time has passed, it causes the chain to stop repeating. The chain is set to repeat for a very large number of repeats so that it will repeat forever until the second Script stops it.
Alternatively, a number could be set as the maximum. For example, if you wanted to repeat for “
” times or until “
” seconds have passed, whichever comes first.
//Get the startTime that was set by the "Record start time" script, from the
// chain property that it was placed into.
var startTime = $context.currentChain.propertyList.getPropertyValue("Start Time");
//Come the elapsed time since repeating started (in milliseconds)
var elapsedTime = Date.now() - startTime;
//If more than 30 seconds since the chain began repeating, terminate clip repeats
if (elapsedTime > 30000)