settimeout - Javascript input timeout -
i've got number of text boxes (dozens) need fire events when user changes values. users have requested not have commit button, , don't want have activate different control event fire. far understand it, means i'm limited running method in oninput
event. problem don't want run method every key stroke-- want wait amount of time before assuming user done typing before commiting change. instance, 1 of input boxes looking number. if user keys in '120' don't want method fire once '1', again '12' third time '120'.
currently have function uses settimeout delay response second user finish inputting info. if user enters character within 1 second window countdown starts over.
function combinetextentries(ctrl, functionobj){ // function prevents text box updating layout until updatedelay time has been reached if (typeof(this.updatedelaytimeout) !== "undefined"){ window.cleartimeout(this.updatedelaytimeout); } this.updatedelaytimeout = settimeout( function(){functionobj();}, 1000); }
i call setting oninput attribute of html to
oninput="combinetextentrieswitharg(this, myfunction)"
the problem running functions require 1 or more arguments. can create different function handler such
function combinetextentrieswitharg(ctrl, functionobj, arg){ if (typeof(this.updatedelaytimeout) !== "undefined"){ window.cleartimeout(this.updatedelaytimeout); } this.updatedelaytimeout = settimeout( function(){functionobj(arg);}, 1000); }
and call this:
oninput="combinetextentrieswitharg(this, myfunction, myarg)"
but seems rather kludgy. can propose better solution? best way passing object arguments enumerated within as specified in answer? hesitation i'd need edit each of functions need call accept single object kind of pain.
you can use apply
make easier pass arguments.
function combinetextentries(ctrl, fnc, args){ if (ctrl.updatedelaytimeout ){ window.cleartimeout(ctrl.updatedelaytimeout ); } ctrl.updatedelaytimeout = settimeout( function(){fnc.apply(this, args);}, 1000); }
and call it
combinetextentries(this, yourfunctionname); combinetextentries(this, yourfunctionname, [argument1]); combinetextentries(this, yourfunctionname, [argument1, argument2]);
Comments
Post a Comment