reading props of a setTimeout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivo

    reading props of a setTimeout

    Hi,
    somewhere in my code a timeout is set in a function:

    var timer = null;
    function foo( n ) {
    var delay = 500;
    // do stuff with n
    timer = window.setTimeo ut( 'foo(' + ( ++n ) + ');', delay );
    }

    Now elsewhere in another function, called during a totally unrelated event,
    this time-out needs to be canceled. Not a problem:

    if( timer ) { window.clearTim eout( timer ); }

    But before erasing the values, I would like to store the string and delay
    parameters used when setting the last timer, to be exact if I know the value
    of n, I can at some time after the cancelling restart the timer at the point
    it was canceled. Saving this information is of course possible in
    traditional ways with more code in the function, more global variables, etc.
    but is there a way to extract the info simply from the setTimeout where it
    resides and which I am dealing with anyway? Such functionality could highly
    simplify lots of work.

    hth
    ivo



  • Jonas Raoni

    #2
    Re: reading props of a setTimeout

    Ivo escreveu:[color=blue]
    > Saving this information is of course possible in
    > traditional ways with more code in the function, more global variables, etc.
    > but is there a way to extract the info simply from the setTimeout where it
    > resides and which I am dealing with anyway? Such functionality could highly
    > simplify lots of work.[/color]

    There are a lot of ways to do it, bellow I've made an example without
    using global variables, I hope it will help you...

    Click on the document to play/pause the timer ;]

    <script type="text/javascript">
    Timer = function(n, interval){
    this.n = n || 0;
    this.interval = interval || 1000;
    this.running = false;
    this._timer = null;
    };
    Timer.prototype .ontimer = function(){
    ++this.n;
    document.title = this.n;
    };
    Timer.prototype .run = function(){
    function getHandler(inst ance){
    return function(){
    instance.ontime r();
    };
    }
    !this._timer && (this._timer = setInterval(get Handler(this),
    this.interval)) ;
    this.running = true;
    };
    Timer.prototype .stop = function(){
    clearTimeout(th is._timer), this._timer = null;
    this.running = false;
    };

    var o = new Timer;
    document.onclic k = function(){
    if(o.running)
    o.stop(), alert("Stopped at " + o.n);
    else
    o.run(), alert("Started at " + o.n);
    };
    </script>


    --
    Jonas Raoni Soares Silva


    Comment

    • Jonas Raoni

      #3
      Re: reading props of a setTimeout

      Lee escreveu:[color=blue]
      > Jonas Raoni said:[color=green]
      > >var o = new Timer;
      > >document.oncli ck = function(){
      > > if(o.running)
      > > o.stop(), alert("Stopped at " + o.n);
      > > else
      > > o.run(), alert("Started at " + o.n);
      > >};
      > ></script>[/color]
      > Maybe I've missed something, but it looks to me as if "o" is a global variable.[/color]

      Awww, don't act like Mr. Spock, I'm just trying to help the other guy,
      I'm not here to discuss superfluous things nor "fight"... =/

      But you're really missed something, not just the "o" is a global var...
      The Timer is one too... But you know everything can be hidden, it's
      quite easy... But in my opinion it isn't worth to write such things:

      (function(){
      var o = new Timer;
      document.onclic k = function(){
      o.running ? (o.stop(), alert("Stopped at " + o.n)) : (o.run(),
      alert("Started at " + o.n));
      };
      })();

      (document.oncli ck = function(){
      var o = arguments.calle e.o;
      o.running ? (o.stop(), alert("Stopped at " + o.n)) : (o.run(),
      alert("Started at " + o.n));
      }).o = new Timer;

      :

      There are a bunch of ways to achieve this... I'll check if I can answer
      another email, then I'll be away, time for the party... I hope Mr.
      Spock to not reply my messages :D


      --
      Jonas Raoni Soares Silva


      Comment

      • Michael Winter

        #4
        Re: reading props of a setTimeout

        On 23/12/2005 14:02, Jonas Raoni wrote:
        [color=blue]
        > Lee escreveu:
        >[color=green]
        >> Jonas Raoni said:
        >>[color=darkred]
        >>> var o = new Timer;
        >>> document.onclic k = function(){
        >>> if(o.running)
        >>> o.stop(), alert("Stopped at " + o.n);
        >>> else
        >>> o.run(), alert("Started at " + o.n);
        >>> };[/color]
        >>
        >> Maybe I've missed something, but it looks to me as if "o" is a
        >> global variable.[/color]
        >
        > Awww, don't act like Mr. Spock [...][/color]

        I think you misunderstand. Assuming Ivo is the same one I recall, he
        would be quite capable of writing an object wrapper from which the
        timeout data could be obtained later. I believe that what he was asking
        for was a known 'getTimeoutData ' function of some sort that, given a
        timer id, would return the arguments passed when creating that timer.

        I doubt that there is such a built-in function anywhere, but you could
        roll your own data structure[1] to fulfil the same goal, rather than a
        simple wrapper.
        [color=blue]
        > But you're really missed something, not just the "o" is a global var...
        > The Timer is one too... But you know everything can be hidden, it's
        > quite easy... But in my opinion it isn't worth to write such things:[/color]

        I should think it depends on the type of code involved. If you intend
        for it to be reused in some other context, it can be quite useful to
        minimise the variables injected into the global object. Other times, as
        you say, it isn't always worth the effort.

        [snip]
        [color=blue]
        > (document.oncli ck = function(){
        > var o = arguments.calle e.o;
        > o.running ? (o.stop(), alert("Stopped at " + o.n)) : (o.run(),
        > alert("Started at " + o.n));
        > }).o = new Timer;[/color]

        An interesting example, but I would stick to the former. Not all user
        agents (Opera, as I recall, is one) implement the callee property (which
        is deprecated in JavaScript, and not defined by ECMA-262).

        [snip]

        Mike


        [1] When I've bothered to look at the actual value returned by
        setTimeout, it's always been a number. If this is /always/
        true, then a simple object could be used instead of a map.

        --
        Michael Winter
        Prefix subject with [News] before replying by e-mail.

        Comment

        • Dr John Stockton

          #5
          reading props of a setTimeout

          JRS: In article <43ab3027$0$258 36$dbd43001@new s.wanadoo.nl>, dated Thu,
          22 Dec 2005 23:58:43 local, seen in news:comp.lang. javascript, Ivo
          <no@thank.you > posted :[color=blue]
          >Hi,
          >somewhere in my code a timeout is set in a function:
          >
          >var timer = null;
          >function foo( n ) {
          > var delay = 500;
          > // do stuff with n
          > timer = window.setTimeo ut( 'foo(' + ( ++n ) + ');', delay );
          >}
          >
          >Now elsewhere in another function, called during a totally unrelated event,
          >this time-out needs to be canceled. Not a problem:
          >
          >if( timer ) { window.clearTim eout( timer ); }
          >
          >But before erasing the values, I would like to store the string and delay
          >parameters used when setting the last timer, to be exact if I know the value
          >of n, I can at some time after the cancelling restart the timer at the point
          >it was canceled. Saving this information is of course possible in
          >traditional ways with more code in the function, more global variables, etc.
          >but is there a way to extract the info simply from the setTimeout where it
          >resides and which I am dealing with anyway? Such functionality could highly
          >simplify lots of work.[/color]

          AFAIK not.

          But, for a long timeout, you could break the delay into individual
          slices - setInterval - keep track of the number of intervals, and use
          that to stop when finished and to restart when interrupted.

          The string and initial delay can be saved by enclosing the initial set
          in a wrapper.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          • Jonas Raoni

            #6
            Re: reading props of a setTimeout

            Lee escreveu:[color=blue]
            > Jonas Raoni said:[color=green]
            > >Awww, don't act like Mr. Spock, I'm just trying to help the other guy,
            > >I'm not here to discuss superfluous things nor "fight"... =/[/color]
            >
            > I don't want to fight, either, but in what way is my pointing out your
            > error more offensive than the attitude you've chosen to take?[/color]

            Sorry, I was a bit crazy when I answered =]


            --
            Jonas Raoni Soares Silva


            Comment

            Working...