Just to clarify the above: Prototype Core already contains a similar
functionality: PeriodicalExecuter. The API is different but the
functionality is the same.

I'd strongly suggest looking into combining both approaches if you
want your suggestion to be included in core and not just stay a thread
in the mailing list. :)

Best,

Tobie

On Jun 24, 4:50 pm, Rick Waldron <waldron.r...@gmail.com> wrote:
> I've subbed my implementation with your to do some use-case testing. I'll
> report back anything of interest as I go along.
>
> Rick
>
> On Wed, Jun 24, 2009 at 10:49 AM, Rick Waldron <waldron.r...@gmail.com>wrote:
>
>
>
> > This is fantastic feedback - thanks!
>
> > On Wed, Jun 24, 2009 at 9:55 AM, Robert Kieffer <bro...@gmail.com> wrote:
>
> >> I can't say I'm a big fan of this.  For several reasons.
>
> >> First, it's just a cosmetic replacement for setInterval(myfunction
> >> (...).bind(), ...) which simply isn't all that bad.
>
> >> Second, I'm not a fan of setInterval in general.  I've seen some
> >> rather nasty behavior with calls queuing up if the invoked function
> >> takes longer than the delay to execute.  In particular, this seems to
> >> be an issue if you do something like put a laptop to sleep.  (But
> >> maybe others haven't seen this problem???)  Thus, I prefer to use a
> >> self-invoking timeout like so:
>
> >> function myFunction() {
> >>    // do stuff ...
> >>    // call ourselves again
> >>    if (/*we want to continue?*/) setTimeout(myFunction, 1000)
> >> }
>
> >> This doesn't call the function at exactly one second intervals, but
> >> that type of accuracy is rarely important.  Instead, it guarantees you
> >> have at least one second of delay between invocations, which for
> >> distributing cpu load or polling (the more common cases where
> >> setInterval might be used), is more desireable.
>
> >> Finally, as Joe T. points out, there should be a way of cancelling the
> >> interval that doesn't require the user to store the returned value
> >> (*that* is what I find most annoying, not the syntax of
> >> "setInterval").
>
> >> Thus, I'd suggest this instead:
>
> >>  Object.extend(Function.prototype, {
> >>    repeat: function(delay) {
> >>      // Reset state
> >>      if (this._repeater) delete this._repeater;
> >>      this._repeatTimeout = clearTimeout(this._repeatTimeout);
>
> >>      if (!delay) return; // (stop repeating if no args or delay==0)
>
> >>      // Create setTimeout-based invoker
> >>      var _method = this;
> >>      if (!this._repeater) this._repeater = function() {
> >>        // Let _method cancel repeat by doing "return false;"
> >>        if (_method() !== false) setTimeout(_method._repeater, delay);
> >>      }
>
> >>      // Start repeating
> >>      this._repeatTimeout = setTimeout(this._repeater, delay);
> >>    },
>
> >>    stopRepeating: function() {
> >>      this.repeat();
> >>    }
> >>  });
>
> >> For example:
>
> >>  var count = 0;
> >>  function foo() {
> >>    console.log(count++);
> >>    return count < 10;  // Return "false" when count >= 10 to cancel
> >> the repeat
> >>  }
>
> >>  // Start repeating 1/sec
> >>  foo.repeat(1000);
> >>  //... some time later change interval to 2/sec
> >>  foo.repeat(500);
> >>  // ... later still stop repeating.
> >>  foo.stopRepeating();
>
> >> As you can see, this implementation of repeat() does a lot more for
> >> you than simply alias'ing "setInterval":
> >>  - It guarantees your function is only invoked by one interval
> >>  - It makes changing the interval or cancelling it altogether
> >> trivial.
> >>  - It allows you to conditionally cancel the repeat from w/in the
> >> function itself.
>
> >> The only thing missing is the bind() behavior but, well, that's what
> >> bind is for.   If you need to bind arguments, just bind() your
> >> arguments first.
>
> >> On Jun 23, 8:25 am, Rick Waldron <waldron.r...@gmail.com> wrote:
> >> > I detest the way setInterval() looks, so I came up with this... have
> >> been
> >> > using it my personal JS for some time.
>
> >> > Object.extend(Function.prototype, {
> >> >   repeat: function() {
> >> >     var __method = this, args = $A(arguments), interval = args.shift() *
> >> > 1000;
> >> >     return window.setInterval(function() {
> >> >       return __method.apply(__method, args);
> >> >     }, interval );
> >> >   }
>
> >> > });
>
> >> > // usage:
> >> > var _pollInt = 0;
> >> > function repetiousPollFn() {
> >> >  console.log(_pollInt++);
>
> >> > }
>
> >> > repetiousPollFn.repeat(.5);
>
> >> > Will, of course, repeat repetiousPollFn() every half second.
>
> >> > Almost identical to .delay(), except that it returns setInterval instead
> >> of
> >> > setTimeout. One thing I intend to add is support for clearInterval,
> >> however
> >> > I figured I'd at least bring it up here first. I've never
> >> > proposed/contributed here before (i'm a lurker of the list :D ) - any
> >> > guidance is appreciated.
>
> >> > Rick
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to 
prototype-core-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to