OK I played around with this for a while with a fresh head. As I understand it, the main power of closures is that they retain the environment in which they were created. I'm not sure how that applies here, since closures have to be defined inside of another function then returned to retain that environment, but I'm sure it's something I'm overlooking.
The problem with the code below is that I need to pass other variables in addition to the event, such as the id and value of the input element that this event handler is being registered on - how can I do this with the below code? Blair Mitchelmore-2 wrote: > > Well first off, my plugin's code can be found at > <http://jquery.offput.ca/js/jquery.every.js> but I haven't yet set up a > page which explains the plug-in's capabilities and I haven't optimised > the code or really debugged it as I wrote it on a lark a while ago and > haven't put much thought to it since. So after all these disclaimers I > suggest you use it with caution. In fact, I've taken the liberty of > re-writing my previous code with regular setTimeouts... > > Example: > var timer; > var fn = function(e) { > if (e && e.type == 'blur') { > if (timer) > window.clearTimeout(timer); > } > // Do stuff > } > $(whatever).blur(fn).keyup(function() { > timer = window.setTimeout(fn,2000); > }).keydown(function() { > if (timer) window.clearTimeout(timer); > }); > > This previous example also takes advantage of another feature you > require: closures. The basic idea behind closures is that you can create > an anonymous function which takes advantage of data currently in scope > even if not existence in the scope the function is ultimately called. > You see this when I create the timer variable and then use it inside the > fn function I created. Any variables in scope when you define your > function can be accessed later on by that function. It's one of the > coolest and most powerful features of JavaScript. Hope all that helped. > > -blair > > Daemach wrote: >> Would you mind posting a link to your timer plugin? >> >> If I'm going to use an external function I will need to pass additional >> parameters. How do you force passing the event type along with extra >> data? >> I'm newish to javascript... >> >> >> >> Blair Mitchelmore-2 wrote: >>> It might be better if you didn't use an anonymous function so you could >>> reference it multiple times. (I'm going to use a plugin I wrote that >>> jQuerizizes timer events cause it really simplifies the syntax of the >>> solution but something equivalent could be done without it.) >>> >>> Example: >>> var fn = function(e) { >>> if (e.type == 'blur') $(this).stop(); >>> // Do stuff >>> } >>> $(whatever).blur(fn).keyup(function() { >>> $(this).once(2000,fn); >>> }).keydown(function() { >>> $(this).stop(); >>> }); >>> >>> Hope that helps. >>> >>> -blair >>> >>> Daemach wrote: >>>> I have a form with which I want to do ajax updates as they type. If >>>> they >>>> stop typing for more than 2 seconds it should update the field. If the >>>> field blurs before the 2 seconds are up it should update the field. >>>> >>>> I have the ajax side of it worked out, and currently the updates work >>>> properly when the field blurs. I just need some ideas on how to write >>>> the >>>> timer function for the keypresses and how it integrates with the blur >>>> function so the function doesn't get called twice and so there are no >>>> memory >>>> leaks from timers set then abandoned because the blur event got to it >>>> first. >>>> >>>> I'm going for elegance here :) I could write an outside function that >>>> gets >>>> called from both event handlers but that seems cheezy. There must be a >>>> way >>>> to do this with an anonymous function... >>> >>> _______________________________________________ >>> jQuery mailing list >>> [email protected] >>> http://jquery.com/discuss/ >>> >>> >> > > > _______________________________________________ > jQuery mailing list > [email protected] > http://jquery.com/discuss/ > > -- View this message in context: http://www.nabble.com/Gmail-style-updates-tf3269331.html#a9104255 Sent from the JQuery mailing list archive at Nabble.com. _______________________________________________ jQuery mailing list [email protected] http://jquery.com/discuss/
