| |
Prototype: Core |
Rick,
What am i missing?
On Jun 24, 12:20 pm, Rick Waldron <waldron.r...@gmail.com> wrote:
> I've posted a demo here:
> All the output is to the firebug console... i've included fbug lite just in
> Rick
> On Wed, Jun 24, 2009 at 10:50 AM, Rick Waldron <waldron.r...@gmail.com>wrote:
> > I've subbed my implementation with your to do some use-case testing. I'll
> > 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
> >>> Second, I'm not a fan of setInterval in general. I've seen some
> >>> function myFunction() {
> >>> This doesn't call the function at exactly one second intervals, but
> >>> Finally, as Joe T. points out, there should be a way of cancelling the
> >>> Thus, I'd suggest this instead:
> >>> Object.extend(Function.prototype, {
> >>> if (!delay) return; // (stop repeating if no args or delay==0)
> >>> // Create setTimeout-based invoker
> >>> // Start repeating
> >>> stopRepeating: function() {
> >>> For example:
> >>> var count = 0;
> >>> // Start repeating 1/sec
> >>> As you can see, this implementation of repeat() does a lot more for
> >>> The only thing missing is the bind() behavior but, well, that's what
> >>> On Jun 23, 8:25 am, Rick Waldron <waldron.r...@gmail.com> wrote:
> >>> > Object.extend(Function.prototype, {
> >>> > });
> >>> > // usage:
> >>> > }
> >>> > repetiousPollFn.repeat(.5);
> >>> > Will, of course, repeat repetiousPollFn() every half second.
> >>> > Almost identical to .delay(), except that it returns setInterval
> >>> > Rick
Maybe i'm missing how that revision works, but it appears to me that
your stop property doesn't actually stop the repeater. Your stop
returns before further execution happens, but the timeout ID for the
window still exists.
-joe t.
> delay() with regard to arguments
> case
> > report back anything of interest as I go along.
> >>> (...).bind(), ...) which simply isn't all that bad.
> >>> 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:
> >>> // do stuff ...
> >>> // call ourselves again
> >>> if (/*we want to continue?*/) setTimeout(myFunction, 1000)
> >>> }
> >>> 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.
> >>> interval that doesn't require the user to store the returned value
> >>> (*that* is what I find most annoying, not the syntax of
> >>> "setInterval").
> >>> repeat: function(delay) {
> >>> // Reset state
> >>> if (this._repeater) delete this._repeater;
> >>> this._repeatTimeout = clearTimeout(this._repeatTimeout);
> >>> var _method = this;
> >>> if (!this._repeater) this._repeater = function() {
> >>> // Let _method cancel repeat by doing "return false;"
> >>> if (_method() !== false) setTimeout(_method._repeater, delay);
> >>> }
> >>> this._repeatTimeout = setTimeout(this._repeater, delay);
> >>> },
> >>> this.repeat();
> >>> }
> >>> });
> >>> function foo() {
> >>> console.log(count++);
> >>> return count < 10; // Return "false" when count >= 10 to cancel
> >>> the repeat
> >>> }
> >>> foo.repeat(1000);
> >>> //... some time later change interval to 2/sec
> >>> foo.repeat(500);
> >>> // ... later still stop repeating.
> >>> foo.stopRepeating();
> >>> 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.
> >>> bind is for. If you need to bind arguments, just bind() your
> >>> arguments first.
> >>> > I detest the way setInterval() looks, so I came up with this... have
> >>> been
> >>> > using it my personal JS for some time.
> >>> > repeat: function() {
> >>> > var __method = this, args = $A(arguments), interval = args.shift()
> >>> *
> >>> > 1000;
> >>> > return window.setInterval(function() {
> >>> > return __method.apply(__method, args);
> >>> > }, interval );
> >>> > }
> >>> > var _pollInt = 0;
> >>> > function repetiousPollFn() {
> >>> > console.log(_pollInt++);
> >>> 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.