I'm new here, so I don't really know if this was made before, but I created short functions to make the job of doing scripts with SetTimeout easier. I found nesting SetTimeout scripts is really messy when you have more than 4 or 5 actions with different delays, so here they are.
<function name="AddToDelayedScriptList" parameters="script, delay">
if (GetObject("delayedScripts") =null) {
create ("delayedScripts", "delayedScriptList")
delayedScripts.dict = delayedScripts.dict
delayedScripts.delays = delayedScripts.delays
delayedScripts.tempString = "0"
}
dictionary add (delayedScripts.dict, ToString(DictionaryCount(delayedScripts.dict)), script)
list add (delayedScripts.delays, ToString(delay))
</function>
<function name="ExecuteDelayedScriptList">
if (DictionaryCount(delayedScripts.dict) = 0) {
delayedScripts.tempString = ToString(0)
delayedScripts.dict = NewScriptDictionary()
delayedScripts.delays = NewStringList()
}
else {
SetTimeout (ToInt(StringListItem(delayedScripts.delays, ToInt(delayedScripts.tempString)))) {
invoke (ScriptDictionaryItem(delayedScripts.dict, delayedScripts.tempString))
dictionary remove (delayedScripts.dict, delayedScripts.tempString)
list remove (delayedScripts.delays, ToString(0))
delayedScripts.tempString = ToString(ToInt(delayedScripts.tempString)+1)
ExecuteDelayedScriptList
}
}
</function>
Important:You also need to create an object type like this:
<type name="delayedScriptList">
<dict type="scriptdictionary" />
<tempString type="string"></tempString>
<delays type="stringlist" />
</type>
Usage:
Just call a number off AddToDelayedScriptList with the script you wish to execute and the delay you want, then call ExecuteDelayedScriptList.
Usually, I do it like this:
s => { msg("TEST")}
AddToDelayedScriptList(s,5)
s => { msg("TEST2")}
AddToDelayedScriptList(s,3)
ExecuteDelayedScriptList()
Output:
After 5 seconds
TEST
After 8 seconds
TEST2
I hope this can be useful to someone.
awesome code/library, this will be of great usefulness for those who need them!