[jQuery] Re: How do you write custom callback functions in jQuery?
Oops, I meant thanks Jorn* On Apr 11, 9:07 pm, "real" <[EMAIL PROTECTED]> wrote: > Oh ok thanks Klaus, I'll look into that. > > On Apr 11, 5:36 pm, "Aaron Heimlich" <[EMAIL PROTECTED]> wrote: > > > On 4/11/07, Klaus Hartl <[EMAIL PROTECTED]> wrote: > > > > Nice! Where do useful micro plugins go? > > > The cookbook[1] perhaps? > > > [1]http://docs.jquery.com/Cookbook(Ido remember there being a lot more > > here, though, when we had the old wiki) > > > -- > > Aaron Heimlich > > Web Developer > > [EMAIL PROTECTED]://aheimlich.freepgs.com
[jQuery] Re: How do you write custom callback functions in jQuery?
Oh ok thanks Klaus, I'll look into that. On Apr 11, 5:36 pm, "Aaron Heimlich" <[EMAIL PROTECTED]> wrote: > On 4/11/07, Klaus Hartl <[EMAIL PROTECTED]> wrote: > > > > > Nice! Where do useful micro plugins go? > > The cookbook[1] perhaps? > > [1]http://docs.jquery.com/Cookbook(I do remember there being a lot more > here, though, when we had the old wiki) > > -- > Aaron Heimlich > Web Developer > [EMAIL PROTECTED]://aheimlich.freepgs.com
[jQuery] Re: How do you write custom callback functions in jQuery?
On 4/11/07, Klaus Hartl <[EMAIL PROTECTED]> wrote: Nice! Where do useful micro plugins go? The cookbook[1] perhaps? [1] http://docs.jquery.com/Cookbook (I do remember there being a lot more here, though, when we had the old wiki) -- Aaron Heimlich Web Developer [EMAIL PROTECTED] http://aheimlich.freepgs.com
[jQuery] Re: How do you write custom callback functions in jQuery?
Klaus Hartl schrieb: I think you wanted to write this: jQuery.fn.delay = function(time, callback) { var self = this; setTimeout(function() { callback.call(self); }, time); }; Thanks Klaus. I shouldn't write code in my email client... Nice! Where do useful micro plugins go? Once upon a time there was a "util" folder in the plugins repository. But that didn't quite work out. -- Jörn Zaefferer http://bassistance.de
[jQuery] Re: How do you write custom callback functions in jQuery?
real schrieb: Not quite, I don't necessarily want a delay in a set amount of time, I was looking to execute the second function after the first one has completed. For animations and AJAX calls, jQuery already handles that, but I wanted to integrate a callback functionality in my own custom methods. The delay is just one example of using callbacks. The basic principle is always the same: Pass in a function reference and call that later. In case you are looking for a way to queue callbacks and automatically call one after the other: jQuery' testsuite[1] contains some code that could give you a good piece to start. I wrote that queuing stuff, let me know if you need any help with that. The intersting methods are synchronize() and process(). stop() and start() is important to run asynchronous tests. var _config = { queue: [], blocking: true, timeout: null, asyncTimeout: 2 // seconds for async timeout }; function synchronize(callback) { _config.queue[_config.queue.length] = callback; if(!_config.blocking) { process(); } } function process() { while(_config.queue.length && !_config.blocking) { var call = _config.queue[0]; _config.queue = _config.queue.slice(1); call(); } } function stop(allowFailure) { _config.blocking = true; var handler = allowFailure ? start : function() { ok( false, "Test timed out" ); start(); }; _config.timeout = setTimeout(handler, _config.asyncTimeout * 1000); } function start() { if(_config.timeout) clearTimeout(_config.timeout); _config.blocking = false; process(); } You add callbacks by passing them to synchronize(). [1] http://dev.jquery.com/browser/trunk/jquery/build/test/data/testrunner.js -- Jörn Zaefferer http://bassistance.de
[jQuery] Re: How do you write custom callback functions in jQuery?
Not quite, I don't necessarily want a delay in a set amount of time, I was looking to execute the second function after the first one has completed. For animations and AJAX calls, jQuery already handles that, but I wanted to integrate a callback functionality in my own custom methods. Thanks for the help though! =) On Apr 11, 2:27 pm, Klaus Hartl <[EMAIL PROTECTED]> wrote: > Jörn Zaefferer schrieb: > > > $.fn.delayHide(time, callbackArgument) { > > var self = this; > > setTimeout(function() { > > callbackArgument.call(self); > > }, time); > > } > > $("input").delay(3000, function() { > > this.hide(); > > }); > > > That should delay hiding an element by three seconds, using a callback. > > Does that example help? > > I think you wanted to write this: > > jQuery.fn.delay = function(time, callback) { > var self = this; > setTimeout(function() { > callback.call(self); > }, time); > > }; > > Nice! Where do useful micro plugins go? > > -- Klaus
[jQuery] Re: How do you write custom callback functions in jQuery?
Jörn Zaefferer schrieb: $.fn.delayHide(time, callbackArgument) { var self = this; setTimeout(function() { callbackArgument.call(self); }, time); } $("input").delay(3000, function() { this.hide(); }); That should delay hiding an element by three seconds, using a callback. Does that example help? I think you wanted to write this: jQuery.fn.delay = function(time, callback) { var self = this; setTimeout(function() { callback.call(self); }, time); }; Nice! Where do useful micro plugins go? -- Klaus
[jQuery] Re: How do you write custom callback functions in jQuery?
real schrieb: I'm pretty sure this is possible, but I couldn't seem to put my finger on how to do it properly. What I'm looking to do is create either functions or jQuery methods with a custom callback functionality, that would work in the same way as the built-in callbacks for fadeIn, slideDown, etc. For example: $.fn.testFunc = function(param, callback){ //do something that delays, like a fadeIn (just using the fadeIn as an example, but since the fadeIn already has callback functionality, it's not an ideal choice, but I hope you get my point) callback(); } So that this could be called like so, and the callback would execute after the rest of the code was finished $(obj).testFunc(param, function(){ //do some more stuff } I've tried tinkering with the $.fn.queue method but I was unable to get it to work for me. $.fn.delayHide(time, callbackArgument) { var self = this; setTimeout(function() { callbackArgument.call(self); }, time); } $("input").delay(3000, function() { this.hide(); }); That should delay hiding an element by three seconds, using a callback. Does that example help? -- Jörn Zaefferer http://bassistance.de