Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Problem Storing Timeout Values In State Variable

In trying to get API-controllable animations to work (despite the irritating bug prohibiting any direct manipulation of animations via the API), I've jury-rigged a solution where I shuffle the animation object between the "map" and "walls" layers to toggle it on and off:      const   flashAnimation  = ( animName )  =>  {          const   animData  =  state . VAMPIRE . Media . animRegistry [ animName ]          if  ( animData . isActive ) {              const   animObj  =  getObj ( "graphic" ,  animData . id )              animObj . set ( "layer" ,  animData . activeLayer )              if  ( animData . timeOut )                  setTimeout (()  =>  {  animObj . set ( "layer" ,  "walls" ) },  animData . timeOut )         }     }      const   pulseAnimation  = ( animName )  =>  {          const   animData  =  state . VAMPIRE . Media . animRegistry [ animName ]          if  ( animData . timer ) {              clearTimeout ( animData . timer )              delete   animData . timer         }          if  ( animData . isActive ) {              const   timeBetween  =  randomInteger ( animData . maxTimeBetween  -  animData . minTimeBetween ) +  animData . minTimeBetween              flashAnimation ( animName )              animData . timer  =  setTimeout (()  =>  {  pulseAnimation ( animName ) },  timeBetween )                         }          sendChat ( "Debug" ,  `/w Storyteller  ${ JSON . stringify ( animData . timer ) } ` )     }      const   activateAnimation  = ( animName ,  minTimeBetween  =  0 ,  maxTimeBetween  =  100000 )  =>  {          const   animData  =  state . VAMPIRE . Media . animRegistry [ animName ]          if  ( animData . timer ) {              clearTimeout ( animData . timer )              delete   animData . timer         }          animData . isActive  =  true          animData . minTimeBetween  =  minTimeBetween          animData . maxTimeBetween  =  maxTimeBetween          setTimeout (()  =>  {               pulseAnimation ( animName )         },  randomInteger ( animData . maxTimeBetween  -  animData . minTimeBetween ) +  animData . minTimeBetween )     } However, I get a JSON error when I grab the timer reference from the state variable to clear it.  Attempting to stringify the result (in the sendChat call, above) also gives me the same error: My understanding of setTimeout is that it returns a numerical ID, so I'm confused as to why there should be a circular reference involved!  Any insights will be much appreciated, and thanks in advance!
1571834715

Edited 1571835576
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
There's a few reasons why this would be happening. The state can only hold simple things. Stuff like objects, arrays, numbers, strings, etc. Additionally, there's no reason to store a timeout in the state anyways because the effects of a timeout call don't persist after the sandbox restarts. Only data that you need between sessions of the sandbox should be stored in the state. Edit: as for timeout's return value, it's called a numerical I'd, but my experience has been that it is some sort of complex object. Perhaps someone more knowledgeable will be able to explain what it actually is. Edit2: also, I don't see where in your code you're actually storing the set timeout return value. And, some code critique of you want it; you should really end each line with a semicolon.