Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Javascript help needed to modify macro

I need a timer which starts and runs in any passages until it runs out (sending the user to the death passage) or until the user does the right thing to stop it. I modified <<timedgoto>> macro, adding a "globtimedgoto" to work in any passage:
version.extensions.timedgotoMacro={major:1,minor:2,revision:0};
macros["goto"]=macros["globtimedgoto"]=macros.timedgoto={timer:null,handler:function(a,b,c,d)
{function cssTimeUnit(s)
{if(typeof s=="string"){if(s.slice(-2).toLowerCase()=="ms"){return +(s.slice(0,-2))||0}else{if(s.slice(-1).toLowerCase()=="s"){return +(s.slice(0,-1))*1000||0}}}throwError(a,s+" isn't a CSS time unit");return 0}
var t,d,m,s;
t=c[c.length-1];d=d.fullArgs();m=0;if(b!="goto"){d=d.slice(0,d.lastIndexOf(t));m=cssTimeUnit(t)}
d=eval(Wikifier.parse(d));
if(d+""&&state&&state.init){if(macros["goto"].timer){clearTimeout(macros["goto"].timer)}
s=state.history[0].passage.title;macros["goto"].timer=setTimeout(function(){if((b=="globtimedgoto")||(state.history[0].passage.title==s)){state.display(d,a)}},m)}}};
It works. Now I need to stop it. I know I should call clearTimeout(ID), but how to get this ID ('macros["goto"].timer') from the script to Twine variable and how to make this call later? This is a very simple question - I just don't know Javascript and its using with Twine well enough.

Comments

  • Well, I simplified everything as much as possible and came to this code:
    <<silently>>
    <<set $globaltimedgoto=
    function(p,t)
    {return setTimeout(state.display(p),t)}
    >>
    <<endsilently>>
    [[Hello?|0]]
    <<set $timer=$globaltimedgoto("timeover",210000)>>
    
    It goes to "timeover" passage, but immediately (the link "Hello" never appears), ignoring the time value! Even replacing "t" variable with a constant 210000 doesn't help. What's wrong?
  • Nobody helped. but I found a solution myself:
    <<set $globaltimedgoto=
    function(p,t)
    {return setTimeout(function(){state.display(p)},t)}
    >>
    
    This works.
Sign In or Register to comment.