| Page 1 of 1 | [ 7 posts ] |
|
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 |
|
Yes, just like setTImeout and setInterval, you have to have the return of the timer to stop it..
|
|
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 Thanks Alex |
Matt ParkerYUI Contributor
|
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 |
|
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 ParkerYUI Contributor
|
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 |
|
Thanks. That's reassuring.
Alex |
| Page 1 of 1 | [ 7 posts ] |
| 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 |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group