[ 7 posts ]

alexlebek

  • Joined: Fri Nov 13, 2009 1:47 am
  • Posts: 67
  • Offline
  • Profile

Y.later control question

Post Posted: Fri Apr 30, 2010 4:49 am
+0-
Hi,

I'm using

var timer = Y. later ( when , o , fn , data , periodic )

Is it possible to control Y.later from within the fn() supplied function? Typically I want to cancel - and exit - when some condition is satisfied - but it seems like I need to have the timer object - timer.cancel(). Is that true?

Thanks
Alex

Dav Glass

  • Username: davglass
  • Joined: Thu Aug 28, 2008 9:28 am
  • Posts: 2088
  • Location: Marion, IL, US
  • Twitter: davglass
  • GitHub: davglass
  • Gists: davglass
  • IRC: davglass
  • Offline
  • Profile

Re: Y.later control question

Post Posted: Fri Apr 30, 2010 7:25 am
+0-
Yes, just like setTImeout and setInterval, you have to have the return of the timer to stop it..

alexlebek

  • Joined: Fri Nov 13, 2009 1:47 am
  • Posts: 67
  • Offline
  • Profile

Re: Y.later control question

Post Posted: Fri Apr 30, 2010 8:10 am
+0-
Ok thanks, I did this :

Code:
var newx=0, newy=0;
var timer = Y.later(100, this, function(dx,dy) {
      if (newx > -960) newx = newx + dx; else newx=0;
      if (newy > -664) newy = newy + dy; else newy=0;
      if (newx < -200) {
                                         // do something else here, then
                                         timer.cancel();
                                     }
}, [-20, -5], true);



This looks odd :) but nice.

Thanks
Alex

Matt Parker

YUI Contributor

  • Username: mattatlamplight
  • Joined: Mon Apr 20, 2009 12:03 pm
  • Posts: 466
  • Location: London UK
  • GitHub: mattparker
  • Gists: mattparker
  • Offline
  • Profile

Re: Y.later control question

Post Posted: Fri Apr 30, 2010 9:01 am
+0-
Hi,

But I've just been playing with this too, and I don't think it works. By the time you reach timer.cancel(); the function has already been called, and the horse has already bolted, as it were, so cancelling it doesn't seem to have any effect.

But let me know if I'm wrong!

Matt

alexlebek

  • Joined: Fri Nov 13, 2009 1:47 am
  • Posts: 67
  • Offline
  • Profile
Tags:

Re: Y.later control question

Post Posted: Fri Apr 30, 2010 11:21 am
+0-
That is why it looked odd to me.

The Y.later is wrapped in an outer function Y.on('click', function(e) {},..) so hasn't the bolted horse taken this outer context with it - so the 'timer' object is in scope and is returned before the fn() is called ? Is there another explanation for why it does/shouldn't work? And if it shouldn't work - any ideas how I should be doing this?

Thanks
Alex

Matt Parker

YUI Contributor

  • Username: mattatlamplight
  • Joined: Mon Apr 20, 2009 12:03 pm
  • Posts: 466
  • Location: London UK
  • GitHub: mattparker
  • Gists: mattparker
  • Offline
  • Profile

Re: Y.later control question

Post Posted: Fri Apr 30, 2010 11:42 am
+0-
I was being a bit dim before; obviously what you're trying to do only makes sense with periodic things - I had that to false.

Anyway, this works for me (ie it only logs to the console once; removing the myTimer.cancel(); line it keeps going:

Code:
var myTimer;
var myFn = function( age ){
    console.log( "function starting " + myTimer.id );
    myTimer.cancel();
    console.log("Hi " + this.name + " , you are " + age );
};

myTimer = Y.later( 1000, { name: "Bob"}, myFn , [ 100 ], true );
 
});

but the last line (Hi Bob...) does get written; the cancel() stops the timeout, but doesn't stop the current function call; it'd need to return; as well to do that, obviously.

Matt

alexlebek

  • Joined: Fri Nov 13, 2009 1:47 am
  • Posts: 67
  • Offline
  • Profile

Re: Y.later control question

Post Posted: Fri Apr 30, 2010 12:35 pm
+0-
Thanks. That's reassuring.

Alex
  [ 7 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum