How about something like (not tested):
myNamespace = {
timer:null,
keyup:function(jqObj){
clearTimeout( myNamespace.timer );
myNamespace.timer = setTimeout(function(){
myNamespace.updateField(jqObj);
}, 6000);
},
updateField:function(jqObj){
// save using jqObj.val();
});
}
// wire up the event handler
$(function(){
$("input").keyup(function(){
myNamespace.keyup( $(this) );
});
}
You may also want to add something in there to prevent a 2nd call to
updateField from running if the last one fired hasn't returned yet.
- Jack
Sean O wrote:
Thanks for the suggestion, but I tried delete upd; before, after, even inside
my upd = setTimeout... function, and nothing happened -- the behavior is
still the same.
SEAN
Andy Matthews-4 wrote:
Since you're setting the value of upd to a function containing the
setTimeout, you should be able to just
delete upd;
-----Original Message-----
From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Sean O
Sent: Wednesday, October 10, 2007 10:39 AM
To: jquery-en@googlegroups.com
Subject: [jQuery] How to clear setTimeout?
Hi,
I'm trying to implement an autoSave function on my web application.
Something that automatically saves the contents of the current field 6
seconds after the last keyUp. It works, but I can't clear the multiple
setTimeouts triggered on each keyUp event.
Example:
$("input").keyup(function(){
var self = this;
var upd = setTimeout( function() {
updateField( $(self) ); // function outside $ scope
to update field contents in dB
},6000);
});
I must've tried about 27 permutations of clearTimeout, setting flag
variables, nullifying variables, etc. Nothing has worked.
To be clear, I need to "restart the countdown" to update the field if a
user
presses another key within 6 seconds of the previous letter. I'd rather
not
be shooting off POSTs on every keystroke ;)
(BTW, I've used the onChange event before, but for text inputs, the user
has
to manually tab/click out of the field to trigger that event)
Thanks,
_________
SEAN O
http://www.sean-o.com
--
View this message in context:
http://www.nabble.com/How-to-clear-setTimeout--tf4601640s27240.html#a1313843
2
Sent from the jQuery General Discussion mailing list archive at
Nabble.com.